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

in #javascript7 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

Sort:  

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

Thank you sir for nice post

so informative post for us.
thanks for sharing

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

nice post haji

Good way to learn Java.

Nice post.

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

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

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

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


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

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

that's pretty cool to know....

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

Seems you have really a good command on JavaScript.

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

Nice post...

Great thanks for the news. Learned something.

Thanks for this technology sharing...... i appreciate this 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.

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

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

Carry on boss carry on

Nice analysis very useful post

Stay blessed Thanks for sharing @ghasemkiani nice post.

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

nice post..
very informative...thanks sir

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

great post. I will try it.
thanks for sharing