簡體   English   中英

用javascript打印字典鍵

[英]printing dictonary keys with javascript

我正在使用以下代碼查看字典UserNames

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

                }

結果很好。 但在編碼時在屏幕上

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

僅顯示 1 個鍵。 為什么?

您不斷地覆蓋#users 的內容而不是添加內容。 這就是我的意思。

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

如果要追加,請改用+=

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM