I have some data which looks like this:
var mydata =[{
"493":{
"name":"Name 1",
"subhere":{
"subhere1":2
}
},
"673":{
"name":"Name 2",
"subhere":{
"subhere1":20
}
}
}];
I want to get the keys value into a variable so I’ve done this:
var data = [];
for (var i = 0; i < mydata.length; i++) {
var obj = mydata[i];
for(var key in obj){
var newObj = {
name: obj[key].name,
y: obj[key].subhere.subhere1,
id: i,
test: obj[key]
};
data.push(newObj);
}
}
If you look at the line that has test: obj[key] that’s the line I want to get the value of the key BUT when I console.log(this.test) I get Object { name="Name 1",…etc but it’s not giving me the 493 or 673
How can I get the key number?
parseIntfor(var key in obj)which means that you are grabbingkeyinobjwhich means thatkeywill be the object property (493,673) andobj[key]will be the value (Object { name="Name 1", ...etc)