How to use Object.keys in forEach

In this article, you will learn about how to use Object.keys() in forEach. In javaScript an Object is a data type that is used to…

The post How to use Object.keys in forEach appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven

In this article, you will learn about how to use Object.keys() in forEach.

In javaScript an Object is a data type that is used to store various types of collection and complex entities. forEach loop is only used on the array. You can not use it to iterate the properties of an Object directly. To do so you have to transform an Object into an array first and you can do it easily by using Object.keys().

Let’s imagine a scenario first. Suppose you have an Student Object where you store student’s name and age. Now by using Object.keys() function you can easily iterate those values. Follow the code example below:

const student = {
  name : 'Deven',
  age : '19'
};

Object.keys(student).forEach(el => {
  console.log(el, student[el]);
});

// Output :
// 'name' 'deven'
// 'age' '19'

Here, when you used Object.keys(student), it will simply return an array of the student object and you know that forEach can iterate the entities of an array. So you can easily get your desired output.

If you want to see the values only then you can use Object.values() function which also returns an array of the Object’s value. See example :

const student = {
  name : 'deven',
  age : '19'
};

Object.values(student).forEach(el => {
  console.log(`${el}`);
});

// Output :
// 'Deven'
// '19

This is all about Object.keys() in javaScript forEach loop. You can get a clear understanding from this article.

The post How to use Object.keys in forEach appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Deven


Print Share Comment Cite Upload Translate Updates
APA

Deven | Sciencx (2021-10-26T07:00:51+00:00) How to use Object.keys in forEach. Retrieved from https://www.scien.cx/2021/10/26/how-to-use-object-keys-in-foreach/

MLA
" » How to use Object.keys in forEach." Deven | Sciencx - Tuesday October 26, 2021, https://www.scien.cx/2021/10/26/how-to-use-object-keys-in-foreach/
HARVARD
Deven | Sciencx Tuesday October 26, 2021 » How to use Object.keys in forEach., viewed ,<https://www.scien.cx/2021/10/26/how-to-use-object-keys-in-foreach/>
VANCOUVER
Deven | Sciencx - » How to use Object.keys in forEach. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/26/how-to-use-object-keys-in-foreach/
CHICAGO
" » How to use Object.keys in forEach." Deven | Sciencx - Accessed . https://www.scien.cx/2021/10/26/how-to-use-object-keys-in-foreach/
IEEE
" » How to use Object.keys in forEach." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/10/26/how-to-use-object-keys-in-foreach/. [Accessed: ]
rf:citation
» How to use Object.keys in forEach | Deven | Sciencx | https://www.scien.cx/2021/10/26/how-to-use-object-keys-in-foreach/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.