the console issues the following object enter image description here every nested object has a gid property. how do i get an array from this property?

gid=[] for(i=0;i<data.length;i++){ for(j=0;j<data.length;j++){ if(data[i][j]=="gid")gid.push(data[i][j]) } } 
  • what's wrong with the code in question? - Grundy
  • @Grundy Cannot read property 'length' of undefined and if I write instead of gid.push (data [i] [j]) I write console.log (data [i] [j]) then undefined - test_q_1
  • obviously data = undefined - you need to run on something else - Grundy
  • $ .each (data, function (key, val) {console.log (val.gid)}) do so - Serge Esmanovich
  • one
    That's actually all of a sudden someone who needs codepen.io/korolariya/pen/Mewbro?editors=1111 - Serge Esmanovich

2 answers 2

data [i] [j] will be Undefined.

 for(i=0;i<data.length;i++){ gid.push(data[i].gid); } 
  • in the code from the question, the length property is called only for data - Grundy

From an object nested in an object that is in an array, you can:

 for (var i = 0; i < data.length; i++){ for(var arg in data[i]){ if(arg === 'gid'){ // какие-то действия; } } } 

I apologize wildly about jquery not searching ... The answer is pure JS ...

  • Brrrrrrrrrrrrrrrrrrr ... - Qwertiy
  • what nonsense, why 2 cycles? And why iterate over each property of an object in a loop? What can you not check for the existence of the desired property and write it down if it exists? Why this second cycle at all? - Vasily Barbashev