0

I have an Object like

let sample = {a:5, b:3 , c:7, d:8}
Object.entries(sample) 

Now i will get an array of length four

[ [a,5], [b,3], [c,7] , [d,8] ] 

Where the key and value will be as an array values.

Now i need to print the values as

  • a holds the value 5
  • b holds the value 3
  • c holds the value 7
  • d holds the value 8
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Learner
  • 8,379
  • 7
  • 44
  • 82

2 Answers2

1

I found the solution:

let sample = {a:5, b:3, c:7, d:8};
for (const [key,value] of Object.entries(sample)) {
  // return whatever you need
  console.log(`${key} holds the value ${value}`)
}

I hope this will solve the issue

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Learner
  • 8,379
  • 7
  • 44
  • 82
1

The answer you posted is technically correct, but it will only work in the most modern among browsers.

For example, Object.entries is not supported by ANY version of IE. Neither is for...of. And the let statement requires at least IE11.

If you care about your code running in older browsers, consider using eg. Object.keys instead of Object.entries, forEach instead of for...of and var instead of let.

This code will do the same thing, but also run fine in IE9 :

var sample = {a:5, b:3 , c:7, d:8};
var keys = Object.keys(sample);

keys.forEach(function(key){
  console.log(key + " holds the value " + sample[key]);
});

And if even the above code is too modern for your taste, you can go for the following approach :

var sample = {a:5, b:3 , c:7, d:8};

for (var key in sample) {
  console.log(key + " holds the value " + sample[key]);
}

That code should work even in IE6!

John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • But `Object.keys` and `forEach` and string interpolation don't work in older browsers either… Why not use a simple `for (var key in sample) { console.log(key + " holds the value " + sample[key]) }` loop – Bergi Dec 11 '17 at 11:56
  • @Bergi : Thanks for your corrections. I'd forgotten about the string interpolation, but fixed my code accordingly. I also added a second version based on `for...in` that should work in even IE6. The reason I didn't mention `for...in` in my initial answer, is because I've learnt to avoid it as a consequence of [**this issue**](https://blog.javascripting.com/2014/09/19/when-should-javascript-developers-use-hasownproperty/). And since we don't need to support IE<9 at my employer's, I've been using `Object.keys` + `forEach` as an alternative to `for...in` + `obj.hasOwnProperty` in all of my code. – John Slegers Dec 11 '17 at 12:17
  • I disagree. We *should* assume that `Object.prototype` is not extended with enumerable properties, just like we assume the native methods to do what we expect. [`obj.hasOwnProperty(…)` is wrong and should not be used](https://stackoverflow.com/a/45014721/1048572). – Bergi Dec 11 '17 at 13:03
  • @Bergi : I agree that `obj.hasOwnProperty` is totally unnecessary when looping over [POJOs](http://g-liu.com/blog/2015/08/object-oriented-programming-javascript-using-pojos-for-good/). That's why I haven't put it in my answer. However, if you're implementing a function or method that allows ANY object, it's better to add `if (obj.hasOwnProperty(key))` by default to prevent unwanted side-effects. – John Slegers Dec 11 '17 at 13:26
  • Still no - if you really don't know your objects and expect them to be unreasonable, you must use `if (Object.prototype.hasOwnProperty.call(obj, key))` – Bergi Dec 11 '17 at 13:31
  • @Bergi : That would be even safer, yes. No disagreement here. Anyway, all I'm saying is that you can avoid this by using `Object.keys` and then looping over the keys... with `forEach`, a regular for-loop or otherwise. – John Slegers Dec 11 '17 at 13:34