0

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'},
    {key: 'banana', value: 'Banana'}
]

Do I just have to make a loop to do this? Is there a better way?

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
masu9
  • 491
  • 8
  • 22

1 Answers1

5

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)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188