我需要创建一个从json对象创建组合框的函数。我无法获得
"use strict"
let vars = {
kagit: {
"ABR": 50,
"Krome 230 gr": 230,
"Krome 300 gr": 300,
"Krome 350 gr": 350,
"Fantezi": 6,
"Kraft": 5,
"1. Hamur": 4
},
ebat: {
"50x70 cm": 5,
"90x126 cm": 76
}
}
从函数中可以看到,我使用object.entries(),但是当我尝试执行函数时,我无法放置从函数执行中返回的参数。
function comboBox(comboValue) {
let getValue = comboValue;
let comboOption = document.querySelector('.' + getValue);
let defaultOption = document.createElement('option');
defaultOption.text = 'Lütfen Seçiniz';
comboOption.add(defaultOption);
comboOption.selectedIndex = 0;
let objEnt = "vars." + getValue;
let option;
for (const [key, value] of Object.entries(objEnt)) {
option = document.createElement('option');
option.text = key;
option.value = value;
comboOption.add(option);
}
return;
}
comboBox("kagit");
答案 0 :(得分:1)
请勿将字符串传递给Object.Entries
。传递对象,相反:
let vars = {
kagit: { "ABR": 50, "Krome 230 gr": 230, "Krome 300 gr": 300, "Krome 350 gr": 350, "Fantezi": 6, "Kraft": 5, "1. Hamur": 4 },
ebat: { "50x70 cm": 5, "90x126 cm": 76 }
}
function comboBox(comboValue) {
// Get the chosen value from the `vars` global;
let objEnt = vars[comboValue];
// Now get this value's entries
let entries = Object.entries(objEnt);
console.log(entries)
}
comboBox("kagit");