JavaScript Basics: Object.keys, Object.values, and Object.entries

in javascript •  7 years ago 

When you call the function Object.keys on an object, it returns an array containing the names of the given object's own, enumerable properties. Contrary to the for...in loop, the inherited properties are not included in the result.

On the other hand, Object.values returns an array containing the values of the given object's own, enumerable properties.

Similarly, Object.entries returns an array which contains the own, enumerable properties of the given object as name and value pairs. Each entry is a two-element array, containing the name and value of a property.

Here is an example:

    let ali = {
        name: "Ali",
        age: 19,
    };

    console.log(Object.keys(ali));    // [ 'name', 'age' ]
    console.log(Object.values(ali));  // [ 'Ali', 19 ]
    console.log(Object.entries(ali)); // [ [ 'name', 'Ali' ], [ 'age', 19 ] ]

In a for...in loop, all the properties in the prototype chain are included in the loop. So if you want to only iterate over own properties, you have to use the hasOwnProperty method or use Object.keys instead.


Related Posts

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

your effort on java is nice. it is informative

simple but useful .. thanks

valuable post for me. Thank you sir.

that is very helpful post...thank you so much

valuable post for me. Thank you sir.

I am impressed by your knowledge of Java Script language. Keep posting and let us know about Java in an easy to learn way. Thanks.

Bisyar khub...

@ghasemkiani WOW. Awesome tutorial. Thanks for sharing. Keep sharing please.

You help us too much, and that's why we learn, make a good contribution and work in this community. You can check my blog and support me with a vote of yours if you like

Excellent informative post for all steemians .Thanks for the sharing informative post. DQmRzwKEGxx96F5rqDP1PsQq3cxAK8GTWvrTtHMG51XMo54_1680x8400.jpeg

nice post friend

I love programming... Also the language of JavaScript. I am using the jQuery in my web design template.

great writing dear @ghasemkiani

This nice post i like it thanks for sharing technology news..

Really great news.... nice post..thanks for sharing@ghasemkiani

  ·  7 years ago (edited)

Thank you sir for nice post

so informative post for us.
thanks for sharing

Good way to learn Java.

I am about to learn javascript after 2 weeks, glad i found your blog .

I really like this post ghasemkiani! keep up the good work!

nice post haji

Nice post.

that's pretty cool to know....

Sir, I was a great fan of your idiom.Now I like your posts with JavaScript.Your posts are helping me to know about javascript.
Your article is the best @ghasemkiani

Helpful post for us...Keep it up.... thanks for share javascript.....

Very useful post about javascript.The post with your javascript is really good.which helps us get a lot of ideas about javascript.
Thank You @ghasemkiani

Thanks for update nice stuff after some days @ghasemkiani
You blog is excellent for ever and you did good work.
My support always with you

Thanks, Very helpful post. I am learning Javascript. Thanks

Seems you have really a good command on JavaScript.

well thanks i have started the series as i thought it can help me too ... to learn

Helpful post for us...Keep it up..Dear..
Carry on your activity...

Good activity that is....that basic complement is need for technology.....carry on

  ·  7 years ago Reveal Comment

@ghasemkiani JavaScript, I just love you. Please keep posting about JavaScript.

Carry on boss carry on

Nice analysis very useful post
Thanks for sharing @ghasemkiani nice post.
Stay blessed

Great thanks for the news. Learned something.

keep posting like this,your really good at this javascript

Your post is good, and I'm re-blog your post. Please upvote me. Thanks

Your blog alweys educational for us

Wonderful, senior

Thanks for this technology sharing...... i appreciate this post......

nice post..
very informative...thanks sir

i'm very sorry dear,, Yesterday I could not come to your blog,,,miss you dear

Nice post...

thanks so much for shared @ghasemkiani :)

Very good and important for java script
Whatever you have knowledge you sharing
That is very good job
I am happy to follow you

Thanks a lot for great information..

Good post on Java.

great post. I will try it.
thanks for sharing