简体   繁体   中英

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM