Javascript地图键的列表

时间:2016-01-06 13:31:41

标签: javascript jquery loops dictionary foreach

我正在尝试在这样的地图上执行forEach

//Initialisation de la map couleur
var mapCouleurs = new Map();
mapCouleurs.set("Blanc", "white");
mapCouleurs.set("Rouge", "red");
mapCouleurs.set("Bleu", "blue");
mapCouleurs.set("Vert", "green");
mapCouleurs.set("Orange", "orange");
mapCouleurs.set("Violet", "purple");
mapCouleurs.set("Jaune", "yellow");
mapCouleurs.set("Gris", "grey");
mapCouleurs.set("Noir", "black");

for (var key in mapCouleurs) {
    document.getElementById("color").innerHTML += "<option value='" + mapCouleurs[key] + "'> " + key + "</option>";
}
<select id='color'></select>
  

我尝试了很多方法,比如带调用的map.forEach()(我认为我并不是真的理解它),甚至是带有$.map$.each的jQuery,但我有没有结果!你能帮我解决一下吗?

3 个答案:

答案 0 :(得分:3)

做这样的事情:

&#13;
&#13;
<select id='select'></select>
&#13;
public interface Foo { 
    bool bar();
    //other stuffs
}


public abstract class FooBar {
    public abstract bool bar();
    //Other stuffs
}

public abstract class FooBarAbstraction: FooBar, Foo {
     //other stuffs
     //Don't supply the interface and abstract here
}

public class FooBarConcrete: FooBarAbstraction {
    public override bool bar() {
        return true;
    }
   //other stuffs
}
&#13;
&#13;
&#13;

答案 1 :(得分:2)

这应该有效:

var colors = new Map();
var select = "";

colors.set("Blanc", "white");
colors.set("Rouge", "red");
colors.set("Bleu", "blue");
colors.set("Vert", "green");
colors.set("Orange", "orange");
colors.set("Violet", "purple");
colors.set("Jaune", "yellow");
colors.set("Gris", "grey");
colors.set("Noir", "black");

for (key of colors.keys()) {
  select += "<option value='" + colors.get(key) + "'> " + key + "</option>\n";
}

的console.log(选择);

答案 2 :(得分:1)

尝试

&#13;
&#13;
//Initialisation de la map couleur
var mapCouleurs = new Map();
mapCouleurs.set("Blanc", "white");
mapCouleurs.set("Rouge", "red");
mapCouleurs.set("Bleu", "blue");
mapCouleurs.set("Vert", "green");
mapCouleurs.set("Orange", "orange");
mapCouleurs.set("Violet", "purple");
mapCouleurs.set("Jaune", "yellow");
mapCouleurs.set("Gris", "grey");
mapCouleurs.set("Noir", "black");

mapCouleurs.forEach(function(value, key)
{
   document.getElementById("color").innerHTML += "<option value='" + value + "'> " + key + "</option>";
})
&#13;
<select id='color'></select>
&#13;
&#13;
&#13;