如何遍历 array/object 并将某些元素添加到 li?
How can I iterate over an array/object and add certain elements to an li?
这是我的第一个问题,如果有点含糊,我们深表歉意。我正在学习 JS,并且我已经在一个问题上停留了几个小时。我得到了一个对象,并被告知将该对象中的某些对象添加到 ul。
{
"dsl": {
"DSL-I-100000": {
"DSL-I-100000": { // Add this to an li
"priority": "1",
"standard": true,
"customer": true,
"internet": true,
"business": true,
"consumer": true,
"filename": "DSL-I-100000.json",
"download": "100",
"vpCategory": "dsli"
}
},
"DSL-IP-100000": {
"DSL-IP-100000": { // Add this to an li
"priority": "1",
"standard": true,
"customer": true,
"internet": true,
"phone": true,
"business": true,
"consumer": true,
"filename": "DSL-IP-100000.json",
"download": "100",
"vpCategory": "dslip"
}
},
"DSL-IP-16000": {
"DSL-IP-16000": { // Add this to an li
"priority": "1",
"standard": true,
"customer": true,
"internet": true,
"phone": true,
"business": true,
"consumer": true,
"filename": "DSL-IP-16000.json",
"download": "16",
"vpCategory": "dslip"
}
},
我的任务是创建一个包含 'metacode' 的列表,正如我的导师所说的那样。那将是'“DSL-I-100000”:',“DSL-IP-100000”:,“DSL-IP-16000”:。最好的方法是什么?我的 JS 知识非常有限,所以请像向傻瓜一样解释。提前谢谢你。
只需从 obj.dsl
获取键并为每个键创建一个 li
元素。
const obj = {
dsl: {
"DSL-I-100000": {
"DSL-I-100000": {
priority: "1",
standard: true,
customer: true,
internet: true,
business: true,
consumer: true,
filename: "DSL-I-100000.json",
download: "100",
vpCategory: "dsli",
},
},
"DSL-IP-100000": {
"DSL-IP-100000": {
priority: "1",
standard: true,
customer: true,
internet: true,
phone: true,
business: true,
consumer: true,
filename: "DSL-IP-100000.json",
download: "100",
vpCategory: "dslip",
},
},
"DSL-IP-16000": {
"DSL-IP-16000": {
priority: "1",
standard: true,
customer: true,
internet: true,
phone: true,
business: true,
consumer: true,
filename: "DSL-IP-16000.json",
download: "16",
vpCategory: "dslip",
},
},
},
};
const ul = document.querySelector("ul");
Object.keys(obj.dsl).forEach(k => {
const li = document.createElement("li");
li.innerText = k;
ul.appendChild(li);
})
<ul>
</ul>
这是我的第一个问题,如果有点含糊,我们深表歉意。我正在学习 JS,并且我已经在一个问题上停留了几个小时。我得到了一个对象,并被告知将该对象中的某些对象添加到 ul。
{
"dsl": {
"DSL-I-100000": {
"DSL-I-100000": { // Add this to an li
"priority": "1",
"standard": true,
"customer": true,
"internet": true,
"business": true,
"consumer": true,
"filename": "DSL-I-100000.json",
"download": "100",
"vpCategory": "dsli"
}
},
"DSL-IP-100000": {
"DSL-IP-100000": { // Add this to an li
"priority": "1",
"standard": true,
"customer": true,
"internet": true,
"phone": true,
"business": true,
"consumer": true,
"filename": "DSL-IP-100000.json",
"download": "100",
"vpCategory": "dslip"
}
},
"DSL-IP-16000": {
"DSL-IP-16000": { // Add this to an li
"priority": "1",
"standard": true,
"customer": true,
"internet": true,
"phone": true,
"business": true,
"consumer": true,
"filename": "DSL-IP-16000.json",
"download": "16",
"vpCategory": "dslip"
}
},
我的任务是创建一个包含 'metacode' 的列表,正如我的导师所说的那样。那将是'“DSL-I-100000”:',“DSL-IP-100000”:,“DSL-IP-16000”:。最好的方法是什么?我的 JS 知识非常有限,所以请像向傻瓜一样解释。提前谢谢你。
只需从 obj.dsl
获取键并为每个键创建一个 li
元素。
const obj = {
dsl: {
"DSL-I-100000": {
"DSL-I-100000": {
priority: "1",
standard: true,
customer: true,
internet: true,
business: true,
consumer: true,
filename: "DSL-I-100000.json",
download: "100",
vpCategory: "dsli",
},
},
"DSL-IP-100000": {
"DSL-IP-100000": {
priority: "1",
standard: true,
customer: true,
internet: true,
phone: true,
business: true,
consumer: true,
filename: "DSL-IP-100000.json",
download: "100",
vpCategory: "dslip",
},
},
"DSL-IP-16000": {
"DSL-IP-16000": {
priority: "1",
standard: true,
customer: true,
internet: true,
phone: true,
business: true,
consumer: true,
filename: "DSL-IP-16000.json",
download: "16",
vpCategory: "dslip",
},
},
},
};
const ul = document.querySelector("ul");
Object.keys(obj.dsl).forEach(k => {
const li = document.createElement("li");
li.innerText = k;
ul.appendChild(li);
})
<ul>
</ul>