Skip to content
Advertisement

Separating (n) keys from array of objects into a single array with keys names

I need to perform filter in the array of objects to get all the keys. Although, whenever there is a obj inside of that key, I would need to get the key name and concat with the key name from the obj, so for example:

const data = [ id: 5, name: "Something", obj: { lower: True, higher: False } ]
result = ["id", "name", "obj.lower", "obj.higher"]

I could manage to do the above code, but, if there is more objs inside the data, I would need to keep adding a if condition inside of my logic, I would like to know if there is any other way, so it doesn’t matter how many objects I have inside the objects, It will concat always.
The code I used from the above mention:

const itemsArray = [
      { id: 1, item: "Item 001", obj: { name: 'Nilton001', message: "Free001", obj2: { test: "test001" } } },
      { id: 2, item: "Item 002", obj: { name: 'Nilton002', message: "Free002", obj2: { test: "test002" } } },
      { id: 3, item: "Item 003", obj: { name: 'Nilton003', message: "Free003", obj2: { test: "test003" } } },
    ];

const csvData = [    
    Object.keys(itemsArray[0]),
    ...itemsArray.map(item => Object.values(item))
].map(e => e.join(",")).join("n")

// Separating keys
let keys = []
const allKeys = Object.entries(itemsArray[0]);
for (const data of allKeys) {
    if (typeof data[1] === "object") {
        const gettingObjKeys = Object.keys(data[1]);
        const concatingKeys = gettingObjKeys.map((key) => data[0] + "." + key);        
        keys.push(concatingKeys);
    } else {
        keys.push(data[0])
    }
}

//Flating
const flattingKeys = keys.reduce((acc, val: any) => acc.concat(val), []);

What I would like to achieve, lets suppose I have this array of object:

const data = 
[
   { id: 10, obj: {name: "Name1", obj2: {name2: "Name2", test: "Test"}}}
   ...
]

Final result = ["id", "obj.name", "obj.obj2.name2", "obj.obj2.test"]

OBS: The first obj contains all the keys I need, no need to loop through other to get KEYS.

I would like to achieve, all the keys from the first object of the array, and if there is objects inside of objects, I would like to concat the obj names (obj.obj2key1)

Advertisement

Answer

You could map the key or the keys of the nested objects.

const
    getKeys = object => Object
        .entries(object)
        .flatMap(([k, v]) => v && typeof v === 'object'
            ? getKeys(v).map(s => `${k}.${s}`)
            : k
        ),
    getValues = object => Object
        .entries(object)
        .flatMap(([k, v]) => v && typeof v === 'object'
            ? getValues(v)
            : v
        ),
    data = { id: 1, item: "Item 001", obj: { name: 'Nilton001', message: "Free001", obj2: { test: "test001" } } },
    keys = getKeys(data),
    values = getValues(data);

console.log(keys);
console.log(values);
.as-console-wrapper { max-height: 100% !important; top: 0; }
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement