• 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:

    • If the targeted property (key) of the object is a primitive value, the original object is returned (do nothing).
    • If the targeted property is an object, the properties of this nested object are merged into the parent object.
    • If the targeted property is an array of objects, each object in the array is merged with the parent object, resulting in an array of merged objects.
    • On key conflicts, the properties of the nested object take precedence over the properties of the parent object.
    • If the targeted property is an array of primitives, each primitive value is added to a copy of the parent object under the original property key.
    • The function also handles arrays of objects, applying the flattening to each object in the array.

    Type Parameters

    • T extends Record<string, any>

      The type of the object or array of objects to be flattened.

    • K extends string | number | symbol

      The key of the property in object T to be flattened.

    Parameters

    • obj: T | T[]

      The object or array of objects to be flattened.

    • key: K

      The key of the property to flatten.

    Returns FlatObjResult<T, K>

    • The flattened object or array of objects.

    Example

    // 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" }

    Example

    // 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" }]

    Example

    // 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