2 回答
TA贡献1866条经验 获得超5个赞
您只需使用地图即可。
const acctData = [
{
"acctNum": "AAA - 1234",
"user": "Alice"
},
{
"acctNum": "AAA - 5231",
"user": "Bob"
},
{
"acctNum": "AAA - 9921",
"user": "Alice"
},
{
"acctNum": "AAA - 8191",
"user": "Alice"
}
];
const balance = {
"AAA - 1234": 4593.22,
"AAA - 9921": 0,
"AAA - 5231": 232142.5,
"AAA - 8191": 4344
};
const newAccount = acctData.map(acc => {
acc.balance = balance[acc.acctNum];
return acc;
});
console.log(newAccount);
TA贡献1784条经验 获得超7个赞
您可以使用.map()和对象解构:
let result = acctData.map(({acctNum, user}) => ({acctNum, user, balance: balance[acctNum]}));
let acctData = [
{
"acctNum": "AAA - 1234",
"user": "Alice"
},
{
"acctNum": "AAA - 5231",
"user": "Bob"
},
{
"acctNum": "AAA - 9921",
"user": "Alice"
},
{
"acctNum": "AAA - 8191",
"user": "Alice"
}
];
let balance = {
"AAA - 1234": 4593.22,
"AAA - 9921": 0,
"AAA - 5231": 232142.5,
"AAA - 8191": 4344
};
let result = acctData.map(({acctNum, user}) => ({acctNum, user, balance: balance[acctNum]}));
console.log(result);
添加回答
举报