0

我正在遍历事务列表并将值推送到数组。

autoArry.push({ 
   id: countTxns,
   txnID: txnID,
   account: buyersAccount
});
doSomething();

function doSomething(){

   var newData = '1234,4567,5678,8900';

  //Loop Here

}

我需要用我的 newData 遍历 autoArry。当我的 newData 与
数组中的 txnID 匹配时,我需要访问与之对应的帐号。

在数组中查找值然后访问与该块相关的所有值的最佳方法是什么?

4

2 回答 2

0

使用 i = 0 到 autoArry.length - 1 的循环,如果 autoArry[i][txnID] = newData 获取 autoArry[i][account] 的值并放入所需的变量中。我希望这是你想要的。

于 2013-08-13T22:10:53.143 回答
0

grep您可以使用and来实现这一点map

//create a map for fast lookups
var newDataMap = {};

$.each('1234,4567,5678,8900'.split(','), function (index, item) {
    newDataMap[item] = true;
});

console.log($.map($.grep(autoArray, function (item) {
    return !!newDataMap[item.txnID];
}), function (item) {
    return item.buyersAccount;
}));

在香草 JS 中:

var newDataMap = {};

'1234,4567,5678,8900'.split(',').forEach(function (item) {
    newDataMap[item] = true;
});

console.log(autoArray.filter(function (item) {
    return !!newDataMap[item.txnID];
}).map(function (item) {
    return item.buyersAccount
}));
于 2013-08-13T22:10:56.493 回答