I have the following array:
{
"red": "Red",
"blue": "Blue"
}
And I want to convert it to a key-value pair like this:
[
{"id":"red", "value":"Red"},
{"id":"blue","value":"Blue"}
]
I have the following array:
{
"red": "Red",
"blue": "Blue"
}
And I want to convert it to a key-value pair like this:
[
{"id":"red", "value":"Red"},
{"id":"blue","value":"Blue"}
]
You can convert the key-value pairs to entries and map their key (id
) and value
to objects.
const colors = {
"red": "Red",
"blue": "Blue"
};
const values = Object.entries(colors).map(([id, value]) => ({ id, value }));
console.log(values);
.as-console-wrapper { top: 0; max-height: 100% !important; }
You can achieve this using Object.entries and map