All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Iterate Object Key, Value

Last Updated : Mar 11, 2024

JavaScript Iterate Object Key, Value

In this tutorial we will show you the solution of JavaScript iterate object key, value, looping through an enumerable dataset is a typical difficulty for programmers.

Arrays, lists, maps, and other objects can be used to store this information. In this tutorial, we'll look at how to solve this problem by utilizing JavaScript to loop through objects and obtain multiple key-value pairs.

Step by step guide on JavaScript iterate object key, value :-

Method 1 - for...in Loop

<html>
<head>
<title></title>
</head>
<body>
<script>
const user = {
    name: 'Mudit Jain',
    email: 'mudit.jain@example.com',
    age: 25,
    dob: '08/02/1989',
    active: true
};
// iterate over the user object
for (const key in user) {
    console.log(`${key}: ${user[key]}`);
}
</script>
</body>
</html>
  1. The for...in the statement is the simplest and most easy way to iterate across object properties. This approach is compatible with all current and older browsers, including Internet Explorer 6 and higher.
  2. Here's an instance of how to iterate over an object using the for...in loop:

Method 2 - Object.keys()

<html>
<head>
<title></title>
</head>
<body>
<script>
const courses = {
    java: 5,
    javascript: 22,
    nodejs: 10,
    php: 15
};
// convert object to key's array
const keys = Object.keys(courses);
// print all keys
console.log(keys);
// [ 'java', 'javascript', 'nodejs', 'php' ]
// iterate over object
keys.forEach((key, index) => {
    console.log(`${key}: ${courses[key]}`);
});
</script>
</body>
</html>
  1. In ES6, the Object.keys() method was introduced. It accepts as a parameter the object you wish to iterate through it and returns an array with all of the properties' names (or keys).
  2. You can then cycle through the array and extract the value of each property using any of the array looping techniques, such as forEach().

Method 3 - Object.values()

<html>
<head>
<title></title>
</head>
<body>
<script>
const animals = {
    tiger: '1',
    cat: '2',
    monkey: '3',
    elephant: '4'
};
// iterate over object values
Object.values(animals).forEach(val => console.log(val));
</script>
</body>
</html>
  1. Object.values() is a new function in ES8 that does the reverse of Object.key(). It returns an array containing the values of all the object's properties.
  2. The values array can then be looped through using any of the array looping methods.

Method 3 - Object.entries()

<html>
<head>
<title></title>
</head>
<body>
<script>
const animals = {
    tiger: '1',
    cat: '2',
    monkey: '3',
    elephant: '4'
};
const entries = Object.entries(animals);
console.log(entries);
// `for...of` loop
for (const [key, value] of Object.entries(animals)) {
    console.log(`${key}: ${value}`);
}
// `forEach()` method
Object.entries(animals).forEach(([key, value]) => {
    console.log(`${key}: ${value}`)
});
</script>
</body>
</html>
  1. Another function introduced in ES8 is Object.entries(), which can be used to navigate an object. Object.entries() creates an array of arrays, unlike Object.values(), which create an array of the object's values.
  2. There are two elements in each inner array. The property is the first element, while the value is the second.
  3. You may use the for...of the loop or the forEach() function to traverse across the array produced by Object.entries(), as seen above.

Conclusion :-

In JavaScript, that's all there is to iterating over object properties. If you wish to iterate the key-value pair in older browsers, for...in is still an excellent solution.

Otherwise, any of the most recent methods might be used: Object.keys(), Object.values(), or Object.entries(). I hope this tutorial on JavaScript iterate object key, value helps you.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪