I am trying to flatten an array of objects. The only real informations i require is the arrays compacted into a single array.
The content is as follows:
const content = [
{ "chocolate" : [1] },
{ "banana" : [5] },
{ "soap" : [2] },
{ "tea" : [4] },
];
All i am interested in is the values in the array. So desired result would be:
const result = [1,5,2,4]
I have tried
Object.keys(content).map((val) => Object.values(content[val]));
and even tried creating a function
const flatten = ({ children = [], ...rest }) => [rest, ...children.flatMap(flatten)];
and calling it like so:
console.log(flatten(content));
but no luck. Help?
content.flatMap(Object.values).flat()orcontent.map(Object.values).flat(2)content.flatMap(obj => Object.values(obj)[0])will do the work.