3 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
你可以做一些事情,比如
const src = [{key:'myKey',value:'myValue'},{key:'mySecondKey',value:'mySecondValue'},{key:'myThirdKey',value:'myThirdValue'},],
result = Object.assign({}, ...src.map(o => ({[o.key]: o.value})))
console.log(result)
.as-console-wrapper{min-height:100%;}

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
其他答案可行,但我覺得它們有點(diǎn)復(fù)雜,這是一個(gè)簡(jiǎn)單的 for of 循環(huán):
const data = [
{
key: 'myKey',
value: 'myValue'
},
{
key: 'mySecondKey',
value: 'mySecondValue'
},
{
key: 'myThirdKey',
value: 'myThirdValue'
}
];
const result = {};
for(const {key, value} of data) {
result[key] = value;
}
console.log(result);
添加回答
舉報(bào)