我有以下 json 和对象。
acctData = [
{
"acctNum": "AAA - 1234",
"user": "Alice"
},
{
"acctNum": "AAA - 5231",
"user": "Bob"
},
{
"acctNum": "AAA - 9921",
"user": "Alice"
},
{
"acctNum": "AAA - 8191",
"user": "Alice"
}
];
balance = {
"AAA - 1234": 4593.22,
"AAA - 9921": 0,
"AAA - 5231": 232142.5,
"AAA - 8191": 4344
};
我想合并它以获得以下响应
[{acctNum: "AAA - 1234", user: "Alice", balance: 4593.22},
{acctNum: "AAA - 5231", user: "Bob", balance: 232142.5},
{acctNum: "AAA - 9921", user: "Alice", balance: 0},
{acctNum: "AAA - 8191", user: "Alice", balance: 4344}]
function myFunction() {
for (var key in acctData) {
for(var name in balance) {
if ( acctData[key].acctNum === name) {
acctData[key]['balance']= balance[acctData[key].acctNum];
break;
}
}
}
}
我这是使用香草javascript的正确方法还是有更有效的方法?
心有法竹
噜噜哒
相关分类