如何将对象列表转换为映射?



我想将具有字段namevalue的对象数组转换为映射,我不知道如何做到这一点。我:

[
{"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);

相关内容

  • 没有找到相关文章

最新更新