简体   繁体   English

如何使用 JS 用嵌套数组填充 HTML 下拉列表?

[英]How can I populate a HTML dropdown with nested arrays, using JS?

I want to populate a dropdown menu using this array.我想使用此数组填充下拉菜单。 I only want the 'planets' to be on the dropdown, not the numbers.我只希望“行星”出现在下拉列表中,而不是数字。 I think it might roughly look like this :我认为它可能大致如下:

for (var i = 0; i < arr.length; i++) {
  var option = document.createElement("OPTION"),
    txt = document.createTextNode(arr[i]);
  option.appendChild(txt);
  option.setAttribute("value", arr[i]);
  select.insertBefore(option, select.lastChild);
}

But I am not sure how to access the planets only...但我不知道如何只访问行星......

var planets = [
  ['Pluto', 0.06],
  ['Neptune', 1.148],
  ['Uranus', 0.917],
  ['Saturn', 1.139],
  ['Jupiter', 2.640],
  ['Mars', 0.3895],
  ['Moon', 0.1655],
  ['Earth', 1],
  ['Venus', 0.9032],
  ['Mercury', 0.377],
  ['Sun', 27.9]
];

Thank you.谢谢你。

You can use array destructuring to pull your inner array into the variables textContent and value immediately, and then apply them to an option element using Object.assign您可以使用array destructuring立即将内部数组拉入变量textContentvalue ,然后使用Object.assign将它们应用于选项元素

planets.forEach( ( [ textContent, value ] ) => { 
  let option = Object.assign(document.createElement( "OPTION" ), {textContent, value});
  select.appendChild( option ) 
});

 var select = document.querySelector("select"), planets = [['Pluto', 0.06],['Neptune', 1.148],['Uranus', 0.917],['Saturn', 1.139],['Jupiter', 2.640],['Mars', 0.3895], ['Moon', 0.1655],['Earth', 1], ['Venus', 0.9032], ['Mercury', 0.377],['Sun', 27.9]]; planets.forEach( ( [ textContent, value ] ) => { let option = Object.assign( document.createElement( "OPTION" ), { textContent, value } ); select.appendChild( option ) });
 <select></select>

I think this is what you are needing:我认为这就是你需要的:

 for(var i = 0; i < arr.length; i++) { var option = document.createElement("option"), // Note that I've added a [0]. That's because arr[i] is still an array that contains the "planet" name ([0]) and the value ([1]) (on first and second position respectively) // I've also added the "var" keyword to avoid generating a global variable with txt var txt = document.createTextNode(arr[i][0]); option.appendChild(txt); // Here too! The [1] was needed to get the value option.setAttribute("value", arr[i][1]); select.insertBefore(option, select.lastChild); }

You have to access the values from the inner array, if you want to get the name or the value.如果要获取名称或值,则必须访问内部数组中的值。

arr[i][0] is the name, and arr[i][1] is the value: arr[i][0]是名称, arr[i][1]是值:

 var arr = [['Pluto', 0.06],['Neptune', 1.148],['Uranus', 0.917],['Saturn', 1.139],['Jupiter', 2.640],['Mars', 0.3895],['Moon', 0.1655],['Earth', 1],['Venus', 0.9032],['Mercury', 0.377],['Sun', 27.9]]; let select = document.querySelector("select") for (var i = 0; i < arr.length; i++) { var option = document.createElement("OPTION"), txt = document.createTextNode(arr[i][0]); option.appendChild(txt); option.setAttribute("value", arr[i][1]); select.insertBefore(option, select.lastChild); }
 <select></select>

Because you have an Array inside of an Array.因为您在数组中有一个数组。 So you have to iterate twice to get all values or once if you want to hard code it.因此,您必须迭代两次以获取所有值,或者如果您想对其进行硬编码,则必须迭代一次。

 for(i = 0; i < arr.length; i++){
     var value = arr[i][0]
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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