I have an array of the form:
[
{
foo:{
name: 'One',
value: 10
},
bar: {
name: 'Two',
value: 20
}
}
]
I want to get an array of the form:
[
{
name: 'One',
value: 10
},
{
name: 'Two',
value: 20
}
]
Can I transform it with a map, for example?
Or do I need another way?
>Solution :
You can use Object.values() along with Array.flatMap()to get the required result:
let arr = [
{
foo:{
name: 'One',
value: 10
},
bar: {
name: 'Two',
value: 20
}
}
]
let result = arr.flatMap(obj => Object.values(obj));
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; top: 0; }