Skip to content

flattenPaths

Functions

flattenPaths()

flattenPaths(object, preservePath, deepFlatten): string[]

扁平化对象属性路径

Parameters

ParameterTypeDescription
objectany目标对象
preservePath(value, key, path) => boolean保留属性路径回调,返回假值时当前路径将不会保存在结果中
deepFlatten(value, key, path) => boolean深度扁平化回调,返回假值时将停止深度递归目标对象

Returns

string[]

Example

ts
const obj = { a: { b: [{ c: [{ d: 1 }] }] } }

flattenPaths(obj)
// => ['a', 'a.b', 'a.b[0]', 'a.b[0].c', 'a.b[0].c[0]', 'a.b[0].c[0].d']

// 仅保留末端属性路径
flattenPaths(obj, (value) => !isObjectLike(value))
// => ['a.b[0].c[0].d']

// 跳过数组属性
flattenPaths(obj, undefined, (value) => isObjectLike(value) && !isArray(value))
// => ['a', 'a.b']