Skip to content

flattenObject

Static Badge

Receive an object obj, perform a depth-first traversal of the enumerable properties of nested objects, and return a flattened new object where the key is the field path string and the value is the corresponding value.

Added in v0.0.4

Usage

ts
import { flattenObject } from 'parsnip-kit'

const input0 = {
  a: [1, 2, { b: 3 }],
  c: {
    d: [4, 5, { e: 6 }]
  }
}
flattenObject(input0)
// {
//   'a[0]': 1,
//   'a[1]': 2,
//   'a[2].b': 3,
//   'c.d[0]': 4,
//   'c.d[1]': 5,
//   'c.d[2].e': 6
// }

const input1 = {
  a: {},
  b: [],
  c: {
    d: {},
    e: []
  }
}
flattenObject(input1) // {}

API

Type Parameter

ArgTypeDescription
Textends objectThe type of the object to be flattened

Arguments

ArgTypeOptionalDefaultDescription
objTfalseundefinedThe object to be flattened

Returns

Type
FlattenNestObject<T>

Reference

FlattenNestObject