Skip to content Skip to sidebar Skip to footer

How To Convert Key Value List Into Array Of Objects?

I have a list of key value pairs like such { apple: 'Apple', banana: 'Banana' } And I would like to convert this into Object like such [ {key: 'apple', value: 'Apple'}

Solution 1:

You can use Object.entries method to get a key-value pair array and Array#map method to iterate and create a customized array.

let obj = {
    apple: "Apple",
    banana: "Banana"
};

let res =Object.entries(obj).map(([key, value]) => ({ key, value }))

console.log(res)

Post a Comment for "How To Convert Key Value List Into Array Of Objects?"