I need to get data from an array, the array looks like this:

array

And I need to get act in a loop from each array

function setAllMap(map, act) { for (var i = 0; i < arrayhome.length; i++) { console.log(arrayhome[i][act]); } } 

Tried to do so, but nothing works

  • Show how you call. It seems to work if passed to the function with the second parameter "act". Also, the text of what is displayed in the console can all clarify - Duck Learns to Hide
  • Add an array fragment to your question, what I see in the screenshot is “object” and access in this case via object [key] .value - diproart

2 answers 2

Most likely var act is not defined. Try arrayhome[i]['act']

  • He has an act there that is passed to the function, possibly conceived) - Duck Learns to Hide
  • thanks, helped - Tit6ka

There are excellent lodash and underscore tools for working with arrays and collections. Iteration over object properties:

 var arrayhome = [{ act: 1 },{ act: 2 }] for(var i in arrayhome) { if (arrayhome.hasOwnProperty(i)) { if(arrayhome[i].act){ document.write(arrayhome[i].act) } } } 

  • thanks, but already helped ['act'] - Tit6ka
  • @ Tit6ka ok. Good luck with your work. - diproart
  • thank you, js is increasingly attracted by various methods - Tit6ka