0

I am trying to group this JSON by using BroadCategory attribute of category

[{
"brand": "Brand3",
"category": {
    "popularity_index": 7,
    "BroadCategory ": "BroadCategory4",
    "MainCategory": "MainCategory410",
    "GeneralCategory": "GeneralCategory41"
},
"description": "colonialism",
"discount": 17,
"id": 9
}, {
"brand": "Brand2",
"category": {
    "popularity_index": 5,
    "BroadCategory ": "BroadCategory2",
    "MainCategory": "MainCategory210",
    "GeneralCategory": "GeneralCategory21"
},
"description": "desc2",
"discount": 15,
"id": 2
}]

I went through underscore.js - _.groupBy nested attribute but this has array inside JSON for location

I tried something like:

var grpArray = _.groupBy(products, function (element) {
   return element.category.BroadCategory;
})

but its not working. Why can't I access BroadCategory ?

1 Answer 1

2

You have to trim a space "BroadCategory "

"BroadCategory ": "BroadCategory2",

Change to:

"BroadCategory": "BroadCategory2",

OR:

_.groupBy(products, function (element) {
   return element.category['BroadCategory '];
})
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh.My bad.That was close. :) Thanks

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.