我有这样的数据json:
{
a: {
content: "value A"
},
b: {
content: "value B",
c: {
content: "value C"
}
}
}
,我想将其显示在这样的列表中:
请帮助我,谢谢
答案 0 :(得分:-1)
我将实现一个遍历对象并生成ul-li内容的函数。
var input = {
a: {
content: "value A"
},
b: {
content: "value B",
c: {
content: "value C"
}
}
};
function getStructure(input) {
var children = "";
for (let key in input) {
if (key === "content") {
children += `<li>${input[key]}</li>`;
} else {
children += `<ul>${getStructure(input[key])}</ul>`;
}
}
return `<ul>${children}</ul>`;
}
document.body.innerHTML += (getStructure(input));