I have an flat object in javascript as
{
top1: {
obj1: {}
obj2: {}
}
top2: {
obj3: {}
obj4: {}
}
top3: {
obj5: {}
obj6: {}
}
}
and I would need to get out an array like
["top1","obj1"],["top1","obj2"],["top2","obj3"],["top2","obj4"],["top3","obj5"],["top3","obj6"]
I felt I could run a run a map as Object.keys(OBJ).map((key) =>
… but I ended in nothing.
Which method could be the best on this case?
3
Answers
You could take a flat map approach with entries and their keys.
If you have deeper nested objects, you could take a recursive approach.
One-liner:
Reference:
Array#flatMap()
Probably unnecessary non-recursive approach for unknown depth of nesting: