gpt4 book ai didi

javascript - 如何获取迭代对象的值

转载 作者:行者123 更新时间:2023-11-29 20:49:25 25 4
gpt4 key购买 nike

我在一个对象中有一个对象,当我想遍历这些对象时。我成功获得了 key ,但没有获得值(value)。这是我的代码:

let oPlaystation = {
product: {
Artikelnummer: 15118903,
Fabrikantcode: 845784935,
Merk: "Sony",
Garantie: "2 jaar",
Garantietype: "Carry-in-garantie"
},
algemeen: {
platform: "Playstation 4",
Aanbevolen_voor_virtual_reality: false,
},
opslagcapaciteit: {
Totale_opslagcapaciteit: "1000 GB",
Werkgeheugen_uitbreidbaar: true,
Maximale_capaciteit_geheugenkaart: "8000 GB"
},
Bedrade_aansluitingen : {
Netwerkaansluiting: true,
HDMI_ansluiting: true,
USB_aansluiting: true,
USB_versie: 3.0,
Aantal_USB_poorten: 2
}
}

let output = "";
Object.keys(oPlaystation).forEach(section => {
output += '<h3>' + section + '</h3>';
output += '<dl class="product-specs">';
let features = Object.keys(oPlaystation[section]);
features.forEach(attr => {
output += '<div class="product-specs__list-item">';
output += '<dt class="product-specs__item-title">' + attr + '</dt>';
output += '<dd class="product-specs__item-spec">' + features[attr] + '</dd>';
output += '</div>';
});
output += '</dl>';
});
document.body.insertAdjacentHTML("beforeend", output);

现在我在 <dd> 中得到 undefined元素作为值。有人知道如何解决这个问题吗?

最佳答案

需要用第一个key再用第二个key取对象

oPlaystation[section][attr]

代替

features[attr]

var oPlaystation = {
product: {
Artikelnummer: 15118903,
Fabrikantcode: 845784935,
Merk: "Sony",
Garantie: "2 jaar",
Garantietype: "Carry-in-garantie"
},
algemeen: {
platform: "Playstation 4",
Aanbevolen_voor_virtual_reality: false,
},
opslagcapaciteit: {
Totale_opslagcapaciteit: "1000 GB",
Werkgeheugen_uitbreidbaar: true,
Maximale_capaciteit_geheugenkaart: "8000 GB"
},
Bedrade_aansluitingen : {
Netwerkaansluiting: true,
HDMI_ansluiting: true,
USB_aansluiting: true,
USB_versie: 3.0,
Aantal_USB_poorten: 2
}
},
output = '';


Object.keys(oPlaystation).forEach(section => {
output += '<h3>' + section + '</h3>';
output += '<dl class="product-specs">';
let features = Object.keys(oPlaystation[section]);
features.forEach(attr => {
output += '<div class="product-specs__list-item">';
output += '<dt class="product-specs__item-title">' + attr + '</dt>';
output += '<dd class="product-specs__item-spec">' + oPlaystation[section][attr] + '</dd>';
output += '</div>';
});
output += '</dl>';
});

document.body.innerHTML += output;

关于javascript - 如何获取迭代对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52660606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com