Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
forEach() method in Javascript for key-value pairs

How to print only the value(s) in the display?

let room1 = new Map();
room1.set("candles", 8)
room1.set("fridge", 1)
room1.set("washing machine", 3)
room1.set("chairs", 4)
room1.set("sofa", 2)
room1.set("jacuzzi", 6)

let text4 = "";
room1.forEach(function(key) {
  text4 += key + "<br>"
});

document.getElementById("demo8").innerHTML = text4;
<p id="demo8"></p>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It's 2022. Forget forEach and use for...of. It gets key-value pairs when iterating over a Map:

let room1 = new Map();
room1.set("candles", 8)
room1.set("fridge", 1)
room1.set("washing machine", 3)
room1.set("chairs", 4)
room1.set("sofa", 2)
room1.set("jacuzzi", 6)

let text4 = "";
for(let [key,value] of room1) {
  text4 += `${key} - ${value}<br>`; // this could be key+" - "+value+"<br>" of course
};

document.getElementById("demo8").innerHTML = text4;
<p id="demo8"></p>

about 4 years ago · Juan Pablo Isaza Report

0

There're some solutions, follow four possibilities...

1) For a better performance use for...of instead forEach:

for(let [key] of room1) {
    text4 += key + "<br>";
};

2) Short code (spread syntax) to get the keys of your Map and convert to Array:

[...room1.keys()].forEach(function(key) {
    text4 += key + "<br>"
});

3) The same of first solution but with Array.from(...) explicit:

Array.from(room1.keys()).forEach(function(key) {
    text4 += key + "<br>"
});

4) Add the first parameter value to forEach function:

room1.forEach(function(value, key) {
    text4 += key + "<br>"
});

Example:

let room1 = new Map();
room1.set("candles", 8)
room1.set("fridge", 1)
room1.set("washing machine", 3)
room1.set("chairs", 4)
room1.set("sofa", 2)
room1.set("jacuzzi", 6)

let display = document.getElementById("demo8");
for (let [key] of room1) {
  display.innerHTML += key + "<br>";
};
<p id="demo8"></p>

Reference:

  • How to use .map() over Map keys in Javascript

  • https://www.geeksforgeeks.org/javascript-map-foreach-method/

about 4 years ago · Juan Pablo Isaza Report

0

You could get only the keys directly.

let rooms = new Map();
rooms.set("candles", 8)
rooms.set("fridge", 1)
rooms.set("washing machine", 3)
rooms.set("chairs", 4)
rooms.set("sofa", 2)
rooms.set("jacuzzi", 6)

console.log([...rooms.keys()]);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!