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

0

2 Answers 2

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

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

Comments

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!

6 Comments

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 : 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. 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.
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.
@Bergi : I agree that obj.hasOwnProperty is totally unnecessary when looping over POJOs. 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.
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))
|

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.