0

I am calling an API which is giving me back, among other things, an array of javascript objects. The objects in the array are named and I need to use the name in the new individual objects I am creating from the array. Problem is, I don't know how to get to the object's name.

{
"OldCrowMine.E9001":{"last_share":1524883404,"score":"0.0","alive":false,"shares":0,"hashrate":0},
"OldCrowMine.S9001":{"last_share":1524,"score":"648.24","alive":true,"shares":632,"hashrate":14317274},
}

I am after the "OldCrowMine.E9001" bit. I am sure this is quite simple, I just don't know how to search for the answer because I am not sure what to call this. I have tried searching for a solution.

enter image description here

SevilNatas
  • 85
  • 1
  • 4

3 Answers3

1

Just loop - or am I missing something? Simplified raw data version.

var raw = {
    "OldCrowMine.E9001":{"share":1524883404},
    "OldCrowMine.S9001":{"share":1524}
};

for(var first in raw) {
    console.log(first +" share -> "+ raw[first]["share"]);
}
Tigger
  • 8,980
  • 5
  • 36
  • 40
0

var obj = {
"OldCrowMine.E9001":{"last_share":1524883404,"score":"0.0","alive":false,"shares":0,"hashrate":0},
"OldCrowMine.S9001":{"last_share":1524,"score":"648.24","alive":true,"shares":632,"hashrate":14317274},
}
console.log(Object.keys(obj)[0]);
Yernar
  • 222
  • 2
  • 10
0

Get the keys and map the name and the object:

var x= {
"OldCrowMine.E9001":{"last_share":1524883404,"score":"0.0","alive":false,"shares":0,"hashrate":0},
"OldCrowMine.S9001":{"last_share":1524,"score":"648.24","alive":true,"shares":632,"hashrate":14317274},
};
var mapped = Object.keys(x).map(function(d,i){return [d,x[d]]});

The name is map[n][0] and its object is map[n][1] where n is your item number.

ibrahim tanyalcin
  • 5,643
  • 3
  • 16
  • 22