[英]Populate child object in Json
I am trying to populate child object from below json on basis of parent id, but facing some issue, I need your help here. 我正在尝试根据父ID从json下方填充子对象,但是面临一些问题,我需要您的帮助。 I am new in Json so please suggest me some solution, I wand to show child1, child2 child3 if page id is 2, I am trying to populate child object from below json on basis of parent id, but facing some issue, I need your help here.
我是Json的新手,所以请向我建议一些解决方案,如果页面ID为2,请尝试显示child1,child2 child3,我试图根据父ID从json下方填充子对象,但面临一些问题,我需要您在这里帮助。 I am new in Json so please suggest me some solution, I wand to show child1, child2 child3 if page id is 2
我是Json的新手,所以请向我建议一些解决方案,如果页面ID是2,则我要显示child1,child2 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"
}
]
}
]
[
{
"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>');
})
});
});
First, filter array to get current page based on id. 首先,过滤数组以获取基于id的当前页面。 And iterate over the properties to create the list.
并遍历属性以创建列表。 For test, pageId is set to 2.
为了进行测试,将pageId设置为2。
$(function(){ var 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" } ] } ] var pageId = 2; var currentPage = json.filter(function(el){ return el.id == pageId; })[0]; $.each(currentPage.subcategories, function(index, obj){ $('#list-category-slider').append('<div class="item"><a href="/' + obj.slug + '">' + obj.title + '</a></div>'); }) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='list-category-slider'> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.