我有以下Json文件,并且我尝试遍历所有内容,但是仅显示嵌套数组中的一项。不知道我在这里做错了什么。
JSON:
{
"items":[
{
"label":"red",
"url":"red",
"items":[
]
},
{
"label":"blue",
"url":"#/blue",
"items":[
{
"label":"green",
"url":"#/green"
},
{
"label":"yellow",
"url":"#/yellow"
},
{
"label":"pink",
"url":"#/pink"
}
]
},......
JS:
var d = document,
main = d.getElementsByTagName('nav')[ 0 ],
ul = d.createElement( 'ul' ),
i;
main.appendChild( ul );
axios.get('../data/colors.json')
.then(function (response) {
console.log(response.data.items);
for( var i in response.data.items ){
var li = d.createElement( 'li' );
if(response.data.items[i].items.length){
li.innerHTML = "<a class='meta'>" + response.data.items[i].label + "<div>" + response.data.items[i].items[i].label + "</div>" + "</a>"; // create a new li element
}
else {
li.innerHTML = "<a class='meta'>" + response.data.items[i].label + "</a>"; // create a new li element
}
ul.appendChild( li );// every time append a new item
}
})
.catch(function (error) {
console.log(error);
});
所需的输出:
<ul>
<li>red<li>
<li>blue
<div>green </div>
<div>yellow </div>
<div>pink</div>
</li>
</ul>
答案 0 :(得分:3)
您将要遍历/循环未使用的嵌套项目。基本上为项目的第二级添加另一个循环。
这是一个清理的版本:http://jsfiddle.net/DennisRas/zyn52p8v/
注意:我删除了.axios模块以显示重要内容。
const data = {
"items":[
{
"label":"red",
"url":"red",
"items":[
]
},
{
"label":"blue",
"url":"#/blue",
"items":[
{
"label":"green",
"url":"#/green"
},
{
"label":"yellow",
"url":"#/yellow"
},
{
"label":"pink",
"url":"#/pink"
}
]
},
{
"label":"blue",
"url":"#/blue",
"items":[
{
"label":"green",
"url":"#/green"
},
{
"label":"yellow",
"url":"#/yellow"
},
{
"label":"pink",
"url":"#/pink"
}
]
},
{
"label":"blue",
"url":"#/blue",
"items":[
{
"label":"green",
"url":"#/green"
},
{
"label":"yellow",
"url":"#/yellow"
},
{
"label":"pink",
"url":"#/pink"
}
]
}
]
};
const nav = document.querySelector('nav');
const ul = document.createElement( 'ul' );
nav.appendChild(ul);
data.items.forEach(function(item) {
const li = document.createElement('li');
let html = "<a class='meta'>" + item.label;
if (item.items && item.items.length) {
item.items.forEach(function(subitem) {
html += "<div>" + subitem.label + "</div>";
});
}
html += "</a>";
li.innerHTML = html;
ul.appendChild(li);// every time append a new item
});
console.log(data.items);
答案 1 :(得分:1)
这是您的操作方法-
var data = {
"items": [{
"label": "red",
"url": "red",
"items": [
]
},
{
"label": "blue",
"url": "#/blue",
"items": [{
"label": "green",
"url": "#/green"
},
{
"label": "yellow",
"url": "#/yellow"
},
{
"label": "pink",
"url": "#/pink"
}
]
}
]
}
var createList = function(response) {
var d = document,
main = d.getElementById('list'),
ul = d.createElement('ul'),
i;
main.appendChild(ul);
//console.log(response.data.items);
for (var i in response.data.items) {
var li = d.createElement('li');
if (response.data.items[i].items.length) {
li.innerHTML = "<a class='meta'>" + response.data.items[i].label + "</a>"; // create a new li element
ul.appendChild(li); // every time append a new item
var innerul = d.createElement('ul');
for (var j in response.data.items[i].items) {
var innerli = d.createElement('li');
innerli.innerHTML = "<div>" + response.data.items[i].items[j].label + "</div>"; // create a new li element
innerul.appendChild(innerli);
}
li = innerul;
} else {
li.innerHTML = "<a class='meta'>" + response.data.items[i].label + "</a>"; // create a new li element
ul.appendChild(li); // every time append a new item
}
ul.appendChild(li); // every time append a new item
}
}
createList({
data: data
});
<div id="list"></div>
答案 2 :(得分:1)
这是我的看法。您将要使用递归来打印列表。
function printList(list,container){
var ul = document.createElement('ul');
list.forEach(function(item){
var li = document.createElement('li');
li.innerHTML = '<div class="meta">' + item.label + '</div>';
if(item.hasOwnProperty('items')){
printList(item.items,li);
}
ul.appendChild(li);
});
container.appendChild(ul);
}
要使用它:
axios.get('../data/colors.json').then(function (response) {
printList(response.data.items,document.body);
})
function printList(list,container){
var ul = document.createElement('ul');
list.forEach(function(item){
var li = document.createElement('li');
li.innerHTML = '<div class="meta">' + item.label + '</div>';
if(item.hasOwnProperty('items')){
printList(item.items,li);
}
ul.appendChild(li);
});
container.appendChild(ul);
}
var items = {
"items":[
{
"label":"red",
"url":"red",
"items":[
]
},
{
"label":"blue",
"url":"#/blue",
"items":[
{
"label":"green",
"url":"#/green"
},
{
"label":"yellow",
"url":"#/yellow"
},
{
"label":"pink",
"url":"#/pink"
}
]
}
]
}
printList(items.items,document.body);
console.log();
答案 3 :(得分:1)
您可以使用一个简单的递归函数来执行此操作,该函数接受要附加到的html元素和一个对象。这种方法的优点是可以工作到任意深度,并且可以保持嵌套而不是将所有内容弄平。大部分工作都是在处理HTML,除了这是一个非常简单的想法:
let obj = {"items":[{"label":"red","url":"red","items":[]},{"label":"blue","url":"#/blue","items":[{"label":"green","url":"#/green"},{"label":"yellow","url":"#/yellow"},{"label":"pink","url":"#/pink"}]}]}
function addItems(parent, obj) {
let keys = Object.keys(obj)
for (key of keys) {
let o = obj[key]
let li = document.createElement('li')
if (Array.isArray(o)) {
let text = document.createTextNode(key);
li.appendChild(text)
let ul = document.createElement('ul')
o.forEach(item => addItems(ul, item))
li.appendChild(ul)
} else {
li.innerText = key + ': ' + o
}
parent.appendChild(li)
}
}
let list = document.getElementById('1')
addItems(list, obj)
<ul id="1">
</ul>
答案 4 :(得分:0)
我不认为您正在遍历嵌套项目数组。您只循环遍历父数组。
如果嵌套数组中有项目,请为其添加另一个循环。例如
for (var j in response.data.items[i].items) {
li.innerHTML = "<a class='meta'>" + response.data.items[i].label + "<div>" + response.data.items[i].items[j].label + "</div>" + "</a>"; // create a new li element
}
我认为有更好的模式可以进行for循环,但我只是将您的模式保留在这里。基本上是根本原因,您不会遍历嵌套项目。