0

I am looping through a transaction list and pushing values to an array.

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

function doSomething(){

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

  //Loop Here

}

I need to loop through the autoArry with my newData. When my newData matches the
txnID in the array, i then need to access the account number that corresponds with it.

What would be the best way to find the value in an array and then access all values tied to that block?

SBB
  • 8,560
  • 30
  • 108
  • 223
  • possible duplicate of [Find object by id in array of javascript objects](http://stackoverflow.com/questions/7364150/find-object-by-id-in-array-of-javascript-objects) – omma2289 Aug 13 '13 at 21:49

2 Answers2

0

Use a loop for i = 0 to autoArry.length - 1, and if autoArry[i][txnID] = newData get the value of autoArry[i][account] and place in the variable you want . I hope this is what you wanted .

0

You can achieve this using 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;
}));

In vanilla 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
}));
plalx
  • 42,889
  • 6
  • 74
  • 90