我想将具有字段name
和value
的对象数组转换为映射,我不知道如何做到这一点。我:
[
{"name": "key1", "value": "value1"},
{"name": "key2", "value": "value2"},
{"name": "key3", "value": "value3"}
]
但是我需要发送请求:
[
{"key1": "value1"},
{"key2": "value2"},
{"key3": "value3"}
]
就用Array.map
:
const output = input.map(
(row) => ({[row.name]: row.value})
);
试试这个:
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);