简体   繁体   中英

printing dictonary keys with javascript

I am using the below code to see dictionary UserNames values

 for (const [key, value] of Object.entries(UserNames)) {
                    console.log(`${key}:`);

                }

the result is good. but on screen when coding

var thekey = ${key};                    
 document.getElementById("users").textContent = thekey.toString();       
    
  

showing just 1 key. why?

You're constantly overwriting the content of #users instead of adding to it. Here's what I mean.

document.getElementById("users").textContent = "key1";
// At this point, #users contains "key1"

document.getElementById("users").textContent = "key2";
// At this point, #users contains "key2"
// This is because you set the content using =,
// which overwrites the previous content

If you want to append, use += instead.

document.getElementById("users").textContent = "key1";
// At this point, #users contains "key1"

document.getElementById("users").textContent += "key2";
// At this point, #users contains "key1key2"
// This is because you set the content using +=,
// which appends to the existing content

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM