3

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 ?

2
  • 1
    Can you show us what you did with _each? Commented Apr 18, 2014 at 7:49
  • @Robert: Artyom's answer should be the fastest. Mine is just another way to do it with underscore. Out of the 3, yours is actually the most readable, IMHO. :) Commented Apr 18, 2014 at 21:38

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thank you for the first answer. I feel I am using the some way like yours though I am using _.each function. I will try Anthony Chu's methods
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.