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?

0

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.