Tuna Tuna - 4 months ago 9
Javascript Question

Lodash loop through an array of arrays

I have an array with other arrays

[ ["1","13052033555","4444444","40000",1461575799,"1"],
["2","13052033555","1111111","30000",1461575884,"1"],
.......]


Question: Using Lodash how can I loop through the values of each inner array and only apply a method to the 5th and 2nd?

Answer

Why do you need lodash? You can do it in plain Javascript. Let's suppose your array is called arr:

for (var i = 0; i < arr.length; i++) {
  arr[i][2].applyMethod();
  arr[i][5].applyMethod();
}

This will apply a method to second and fifth element of each array.

If you want, instead (sorry, your question isn't clear), apply a method to each element of the second and fifth array you do something like this:

for (let i = 0; i < arr[2].length; i++) {
  arr[2][i].applyMethod();
}

for (let i = 0; i < arr[5].length; i++) {
  arr[5][i].applyMethod();
}
Comments