I am trying to simplify an array with Javascript
[{
{name: orange,
quanity: 1},
{name: apple,
quanity: 3}
}]
I want
[{
orange:1,
apple: 3
}]
I have tried
const keys = flat.map(item => {
item.name = item.quanity
})
But I get
[ undefined, undefined ]
Thanks!
>Solution :
You can return the key-value pairs you want in the final object from the map callback, then use Object.fromEntries to create the combined object.
let arr=[{name:"orange",quanity:1},{name:"apple",quanity:3}];
let res = [Object.fromEntries(arr.map(o => [o.name, o.quanity]))];
console.log(res);