DEV Community

Cover image for forEach( ): Object.values / Object.keys
Luca
Luca

Posted on

3 1

forEach( ): Object.values / Object.keys

Hi!🙋‍♂️

I wrote this article for show you this 3 forEach ways:

Take this simply array:

var array = [
    {
        name: 'John'
    },
    {
        name: 'Mary'
    }
];
Enter fullscreen mode Exit fullscreen mode

If you want to cycle the element with forEach() method, you have three ways:

Keys:

Object.keys

Object.keys(array).forEach(function(key) {
    console.log(key); //--> 0 1
});
Enter fullscreen mode Exit fullscreen mode

Values:

Object.values

Object.values(array).forEach(function(value) {
    console.log(value);  //--> name: 'John' name: 'Mary'
    console.log(value.name);  //--> John Mary
});
Enter fullscreen mode Exit fullscreen mode

forEach:

.forEach

array.forEach(function (val) {
    console.log(val); //--> name: 'John' name: 'Mary'
})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay