Skip to content

flatten

The flatten function recursively traverses a nested structure, such as an object, array, set, or map, and returns a flattened version of the structure. This means that it removes any nested references and produces a structure containing no refs.

To flatten a nested structure, simply pass the reference to the flatten function. It will return a new structure with all nested references removed.

import { flatten, ref } from 'regor'
const nestedData = {
name: 'John',
age: ref(30),
hobbies: ['reading', 'swimming', ref('drawing')],
}
const flattenedData = flatten(nestedData)
console.log(flattenedData)
// Outputs: { name: 'John', age: 30, hobbies: [ 'reading', 'swimming', 'drawing' ] }
  • reference: The nested structure (object, array, set, map) that you want to flatten. It can contain nested refs, which will be resolved during flattening.
  • The flatten function returns a new structure that is a flattened version of the input reference. Any nested refs within the structure are resolved, resulting in a single-level structure.
  • The flatten function recursively flattens nested structures, including arrays, sets, maps, and objects.

  • If the input reference contains refs, they are automatically unrefed during flattening, and their values are included in the flattened structure.

  • Circular references within the input reference are not supported, and attempting to flatten such structures may result in unexpected behavior.

import { flatten, ref } from 'regor'
const nestedData = {
name: 'John',
age: ref(30),
hobbies: ['reading', 'swimming', ref('drawing')],
}
const flattenedData = flatten(nestedData)
console.log(flattenedData)
// Outputs: { name: 'John', age: 30, hobbies: [ 'reading', 'swimming', 'drawing' ] }

Back to the API list