下划线从JSON对象创建对象数组

时间:2014-04-18 07:46:12

标签: javascript underscore.js

我有以下JSON结构:

  {
        "shops": {
            "categories": {
                "cat_1": {
                    "id": "1",
                    "label": "Men's Fashions",
                    "Brands": [{
                        "id": "2",
                        "name": "Smith"
                    }]
                },
                "cat_2": {
                    "id": "2",
                    "label": "Restaurants",
                    "Brands": [{
                        "id": "3",
                        "name": "KFC"
                    }, {
                        "id": "4",
                        "name": "SUBWAY"
                    }, {
                        "id": "5",
                        "name": "MLD"
                    }, {
                        "id": "6",
                        "name": "THAI"
                    }]
                },
                "cat_3": {
                    "id": "3",
                    "label": "Specialty Shops",
                    "Brands": [{
                        "id": "7",
                        "name": "BODY SHOP"
                    }]
                }
            }
        }
    }

我想要达到这样的目标:

    [{
            "categoryid": "1",
            "id": "2",
            "label": "Men's Fashions",
            "name": "Smith"
        },

        {
            "categoryid": "2",
            "id": "3",
            "label": "Restaurants",
            "name": "KFC"
        },

        {
            "categoryid": "2",
            "id": "4",
            "label": "Restaurants",
            "name": "SUBWAY"
        },

        {
            "categoryid": "2",
            "id": "5",
            "label": "Restaurants",
            "name": "MLD"
        },

        {
            "categoryid": "2",
            "id": "6",
            "label": "Restaurants",
            "name": "THAI"
        }, {
            "categoryid": "3",
            "id": "7",
            "label": "Specialty Shops",
            "name": "BODY SHOP"
        },

    ]

使用下划线是否有一种优雅的方式来实现它?

我尝试使用嵌套的_.each()来做到这一点,但觉得可能会有更好的东西。

 generateArray: function(obj) {

  var newResult = [];

    _.each(obj.categories, function(c) {

        _.each(c.Brands, function(d) {

            newResult.push({

                "categoryid": c.id,
                "id": d.id,
                "label": c.label,
                "name": d.name

            });

        });


    });
    return newResult;

}

任何人都可以告诉我哪种方式在运行时更有效率?

我或@Artyom Neustroev或@Anthony Chu?

2 个答案:

答案 0 :(得分:4)

你真的不需要underscore来完成这项任务。使用简单的for .. in ..for (...)循环:

var json = {...};
var result = [];    
for (var catKey in json.shops.categories) {
   var currentCategory = json.shops.categories[catKey];
   for (var i = 0; i < currentCategory.Brands.length; i++) {
      var currentBrand = currentCategory.Brands[i];
      result.push({
         categoryid: currentCategory.id,
         label: currentCategory.label,
         id: currentBrand.id,
         name: currentBrand.name
      });
   }
}

<强> Fiddle here

答案 1 :(得分:2)

而不是each(),这是使用map()的方法来实现的...

var output = _.chain(input.shops.categories)
    .map(function (category) {    
        return _(category.Brands).map(function (brand) {
            return { categoryId: category.id,
                    id: brand.id,
                    label: category.label,
                    name: brand.name
            };
        });
    }).flatten().value();

JSFIDDLE