How can I iterate through keys and values in JavaScript

0 votes
With the help of code, can you tell me How I can iterate through keys and values in JavaScript?
Jan 10 in Java-Script by Ashutosh
• 20,870 points
98 views

1 answer to this question.

0 votes

In JavaScript , you can iterate through keys and values using these methods:

1.Using Object.keys()

const obj = { a: 1, b: 2, c: 3 };

Object.keys(obj).forEach(key => {

  console.log(key, obj[key]); // key and value

});

2. Using Object.entries()

const obj = { a: 1, b: 2, c: 3 };

for (let [key, value] of Object.entries(obj)) {

  console.log(key, value); // key and value

}

answered Feb 7 by Navya

Related Questions In Java-Script

0 votes
1 answer

How can I get query string values in JavaScript?

Hello @kartik, You don't need jQuery for that ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,840 points
1,523 views
0 votes
1 answer

How can I iterate through the properties of a JavaScript object?

You can iterate through the properties of ...READ MORE

answered Jan 10 in Java-Script by Navya
113 views
0 votes
2 answers

How can I set a session var using javascript and get it via php code?

Yes it is possible. You can either ...READ MORE

answered Aug 9, 2020 in Java-Script by Okugbe
• 280 points
29,315 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How can I replace every instance of a string in JavaScript?

In JavaScript, replacing all instances of a ...READ MORE

answered Jan 10 in Java-Script by Navya
107 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP