我想在父ID的基礎上從下面的JSON填充子對象,但面臨一些問題,我需要你的幫助。我是Json的新人,所以請給我建議一些解決方案,我魔杖顯示child1,child2 child3如果頁面ID是2,我想從父母id的基礎上填充子對象從下面的json,但面臨一些問題,我需要你幫助這裏。我以JSON很新,所以請建議我一些解決方案,我的魔杖展示child1,的child2 child3如果頁面ID爲2在Json中填充子對象
[
{
"id": "2",
"slug": "parent",
"title": "Parent",
"subcategories": [
{
"id": "12",
"slug": "child1",
"title": "child1"
},
{
"id": "14",
"slug": "child2",
"title": "child2"
},
{
"id": "15",
"slug": "child3",
"title": "child3"
},
{
"id": "16",
"slug": "child4",
"title": "child4"
}
]
},
{
"id": "11",
"slug": "parent2",
"title": "Parent2",
"subcategories": [
{
"id": "32",
"slug": "child1",
"title": "child1"
},
{
"id": "33",
"slug": "child2",
"title": "child3"
}
]
}
]
[
{
"id": "2",
"slug": "parent",
"title": "Parent",
"subcategories": [
{
"id": "12",
"slug": "child1",
"title": "child1"
},
{
"id": "14",
"slug": "child2",
"title": "child2"
},
{
"id": "15",
"slug": "child3",
"title": "child3"
},
{
"id": "16",
"slug": "child4",
"title": "child4"
}
]
},
{
"id": "11",
"slug": "parent2",
"title": "Parent2",
"subcategories": [
{
"id": "32",
"slug": "child1",
"title": "child1"
},
{
"id": "33",
"slug": "child2",
"title": "child3"
}
]
}
]
$.getJSON("data.json" , function(json) {
$.each(json,function(i, value){
$.each(value.subcategories, function(index, obj){
$('#list-category-slider').append('<div class="item"><a href="/' + obj.slug + '">' + obj.title + '</a></div>');
})
});
});
對不起錯字的錯誤,這將是孩子 – Sajal
您的代碼爲我工作。 –