Pregunta

I have following JSON structure:

  {
        "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"
                    }]
                }
            }
        }
    }

I'd like to achieve something like this:

    [{
            "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"
        },

    ]

Is there an elegant way to achieve it using underscore?

I tried to use nested _.each() to do that, but feel there might be something better.

 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;

}

Anyone can advise me which way is more efficiency at running time?

mine or @Artyom Neustroev or @Anthony Chu ?

¿Fue útil?

Solución 2

Instead of each()'s, here's a way to do it with map()'s...

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

Otros consejos

You don't really need underscore for that task. Use simple for .. in .. and for (...) loops:

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top