【问题标题】:Populate child object in Json在 Json 中填充子对象
【发布时间】:2017-08-17 18:34:46
【问题描述】:

我正在尝试根据父 ID 从下面的 json 填充子对象,但遇到一些问题,我需要您的帮助。我是 Json 的新手,所以请建议我一些解决方案,如果页面 ID 为 2,我想显示 child1,child2 child3,我正在尝试根据父 ID 从 json 下面填充子对象,但面临一些问题,我需要你的在这里帮助。我是 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>');
        })
    });
});

【问题讨论】:

  • 对不起,错字,是孩子
  • 你的代码对我有用。

标签: javascript jquery arrays json


【解决方案1】:

首先,过滤数组以根据 id 获取当前页面。并遍历属性以创建列表。为了测试,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>

【讨论】:

  • 请接受答案,并从 json 和问题本身中删除重复项。
猜你喜欢
  • 2018-08-11
  • 2020-07-10
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 2023-03-21
  • 2018-09-09
相关资源
最近更新 更多