如何在JSON中选择特定对象

布里吉什·甘地

我有如下的JSON数据

{
"items":
    {
        "item":
            [
                {
                    "id": "0001",
                    "type": "donut",
                    "name": "Cake",
                    "ppu": 0.55,
                    "batters":
                        {
                            "batter":
                                [
                                    { "id": "1001", "type": "Regular" },
                                    { "id": "1002", "type": "Chocolate" },
                                    { "id": "1003", "type": "Blueberry" },
                                    { "id": "1004", "type": "Devil's Food" }
                                ]
                        },
                    "topping":
                        [
                            { "id": "5001", "type": "None" },
                            { "id": "5002", "type": "Glazed" },
                            { "id": "5005", "type": "Sugar" },
                            { "id": "5007", "type": "Powdered Sugar" },
                            { "id": "5006", "type": "Chocolate with Sprinkles" },
                            { "id": "5003", "type": "Chocolate" },
                            { "id": "5004", "type": "Maple" }
                        ]
                },
                {
                    "id": "0002",
                    "type": "donut",
                    "name": "Raised",
                    "ppu": 0.55,
                    "batters":
                        {
                            "batter":
                                [
                                    { "id": "1001", "type": "Regular" }
                                ]
                        },
                    "topping":
                        [
                            { "id": "5001", "type": "None" },
                            { "id": "5002", "type": "Glazed" },
                            { "id": "5005", "type": "Sugar" },
                            { "id": "5003", "type": "Chocolate" },
                            { "id": "5004", "type": "Maple" }
                        ]
                }]
   }
  }

我想选择item具有id的对象0001。那么如何item使用javascript或jQuery只选择或过滤单个对象呢?

请帮助我,在此先感谢

光盘..

像这样:

var item = data.items.item.filter(function(x){ 
  return x.id === '0001'; 
})[0];

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章