js从数组中根据id获取value
例如有如下数据结构:
如何通过rels的数组的fromNodeId和toNodeId查找nodes的accountName
{
"data": {
"nodes": [
{
"id": 11,
"nodeId": 23654,
"accountNo": "9224100105243",
"accountName": "卫恍恬",
"credentialNo": "",
"accountOrg": "313662000082"
}
],
"rels": [
{
"netAmount": 190849,
"fromNodeId": 36495,
"toNodeId": 36496
}
]
},
"code": 200,
"msg": "success"
}
实现代码:
for (var i = 0; i < result.data.nodes.length; i++){
// alert(result.data.nodes[i].nodeId);
if (result.data.nodes[i].nodeId == item.fromNodeId){
fromNodeName=result.data.nodes[i].accountName;
}else if(result.data.nodes[i].nodeId == item.toNodeId){
toNodeName=result.data.nodes[i].accountName;
}
}