The type of the object or array of objects to be flattened.
The key of the property in object T to be flattened.
The object or array of objects to be flattened.
The key of the property to flatten.
// Flattening a single object with a nested object
const obj = { prop1: 1, data: { prop2: "value1" } };
const flat = flatObj(obj, "data");
// Result: { prop1: 1, prop2: "value1" }
// Flattening an object with a nested array
const obj = { prop1: 1, data: [{ prop2: "value1" }, { prop2: "value2" }] };
const flat = flatObj(obj, "data");
// Result: [{ prop1: 1, prop2: "value1" }, { prop1: 1, prop2: "value2" }]
// Flattening an array of objects with nested arrays
const objs = [{ prop1: 1, data: ["a", "b"] }, { prop1: 2, data: ["c", "d"] }];
const flatArray = flatObj(objs, "data");
// Result: [{ prop1: 1, data: "a" }, { prop1: 1, data: "b" }, { prop1: 2, data: "c" }, { prop1: 2, data: "d" }]
Generated using TypeDoc
Flattens a nested object or an array of objects based on a specified key. The function 'pulls down' properties from the nested object or objects in an array into the parent object. This is particularly useful for flattening data structures with nested objects or arrays.
Behavior:
key
) of the object is a primitive value, the original object is returned (do nothing).