0

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.

1
  • for (var key in d) { console.log(key, d[key]) } Commented Nov 8, 2016 at 1:26

4 Answers 4

1

If you want to use a for/in loop, there is no better way to write this. You have to keep your own loop variable to count the iterations.


You could use Object.keys to get an array of keys first and then access their index either by using a for loop or using forEach, but I wouldn't call this "better", it's just a different way to go about it:

var d = {KpiName:"KPI1",  '1/1/2016':"85%",  '1/2/2016':"87%"};

Object.keys(d).forEach(function(key, i) {
  console.log("d[\"key\"]: ", d[key]);
  console.log("i: ", i);
});

Sign up to request clarification or add additional context in comments.

Comments

1

I don't know if I get your question correctly, but you can use Object.keys with forEach:

var d={KpiName:"KPI1",  '1/1/2016':"85%",  '1/2/2016':"87%"};

Object.keys(d).forEach(function(e, i){
  console.log("the key is: " + e + ", the value is: " + d[e] + ", the index is: " + i)
})

Comments

1

I have created some code that will print nicely what you are looking for. The trick is to use Object.keys because it passes the index for you. Here is a clean snippet of code that looks like it supports your use case:

var d = { KpiName:"KPI1",  '1/1/2016':"85%",  '1/2/2016':"87%"}

Object.keys(d).forEach(function (key, index) {
  if (index == 0) {
    console.log("  ",key,"        ",d[key])
    console.log("------------------------")
  } else {
    console.log( index + ". " + key + "          "+d[key]);
  }
});

// run then open console to view output

//    KpiName       KPI1
// ------------------------
// 1. 1/1/2016      85%
// 2. 1/2/2016      87%

I have provided a working example here: https://jsfiddle.net/h9yw48bz/

Please shoot me a +1 if you find this to be a helpful answer

Comments

0

There are so many options as below to achieve this as below.

Method 1:

If you target only the modern browsers and not an old IE you can use Object.keys as below:

var d = {KpiName:"KPI1",  '1/1/2016':"85%",  '1/2/2016':"87%"};
//var i = 0;
Object.keys(d).forEach(function(key, i){
   console.log('d["' + key + '"]: ' + d[key]); 
   console.log('index: ' + (i++).toString());
});

Method 2:

If you target old browsers as well, you can use the same one you did above with a minor change not to include any inherited object properties:

var d = {KpiName:"KPI1",  '1/1/2016':"85%",  '1/2/2016':"87%"};
var i = 0;
for(var key in d) {
  if(d.hasOwnProperty(key)){
    console.log('d["' + key + '"]: ' + d[key]); 
    console.log('index: ' + (i++).toString());
  }
}

Method 3:

Using Jquery

var d = {KpiName:"KPI1",  '1/1/2016':"85%",  '1/2/2016':"87%"};
var i = 0;
$.each(d, function(key, value) {
  console.log('d["' + key + '"]: ' + value); 
  console.log('index: ' + (i++).toString());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Method 4:

Using AngularJS

var d = {KpiName:"KPI1",  '1/1/2016':"85%",  '1/2/2016':"87%"};
    var i = 0;
    angular.forEach(d, function(value, key) {
      console.log('d["' + key + '"]: ' + value); 
      console.log('index: ' + (i++).toString());
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

1 Comment

Why did you mention jQuery and AngularJS, but not lodash, underscore, iter, or any other library that provides a way to iterate over values?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.