'Get randomly generated index number (Property name) of a nested JavaScript Array and put into a variable

I have a variable with 4 indexes (nt_data). What I need to do it to be able to grab the numbers highlighted in green and add them into another variable or array. enter image description here

when I try nt_data[0] it'll list all the details including one of the numbers I want to grab (number 11):

enter image description here

and when I try nt_data[0][11] it'll show me the details inside index number 11 (which isn't an index):

enter image description here

what I need to do is to grab all of those numbers (11, 12, 3, 8) and put them into a variable so I can use them dynamically to point to that index. these numbers are randomly generated and change each time.



Solution 1:[1]

You are trying to get key from Object, The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.

Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually.

Here's the example of retrieve key from object array:

let nt_data = [
  {11: 'a'}, {12: 'b'}, {3: 'c'}, {8: 'd'}
];

for(index in nt_data){ console.log(Object.keys(nt_data[index])[0]) }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J.C. Fong