I want to access the an object keys and the index from an object, what is the best way to do this?
this is data object:
d={KpiName:"KPI1", '1/1/2016':"85%", '1/2/2016':"87%"}
Object {KpiName: "KPI1", 1/1/2016: "85%", 1/2/2016: "87%"}
this is my for 1st loop with output
for (var key in d) { console.log("d[\"key\"]: ", d[key]) }
d["key"]: KPI1
d["key"]: 85%
d["key"]: 87%
this is what I want to achieve but I thought I could write it better
2nd for loop with output
i=1; for (var key in d) { console.log("d[\"key\"]: ", d[key]); console.log("i: ", i); i++}
d["key"]: KPI1
i: 1
d["key"]: 85%
i: 2
d["key"]: 87%
i: 3
3
I thought I could write the for loop like this
for (var key, i in d){...}
But it does not seem to work, maybe my 2nd for loop achieves what I want but I am not sure if it the best code.
for (var key in d) { console.log(key, d[key]) }