I want to convert array of objects with fields name
and value
into map and I have no idea how to do this.
I get:
[
{"name": "key1", "value": "value1"},
{"name": "key2", "value": "value2"},
{"name": "key3", "value": "value3"}
]
But I need to send in request:
[
{"key1": "value1"},
{"key2": "value2"},
{"key3": "value3"}
]
CodePudding user response:
Just use Array.map
:
const output = input.map(
(row) => ({[row.name]: row.value})
);
CodePudding user response:
Try this :
const arr = [
{"name": "key1", "value": "value1"},
{"name": "key2", "value": "value2"},
{"name": "key3", "value": "value3"}
];
const res = arr.map((obj) => {
return {[obj.name]: obj.value}
});
console.log(res);