I have an incoming Json list that looks like the below
[{
"id": 190,
"title": "This is a Story title",
"category": [{
"id": 43,
"title": "XXX",
"titleFr": "YYY"
}, {
"id": 27,
"title": "AAA",
"titleFr": "BBB"
}]
}, {
"id": 191,
"title": "This is a Story title 2",
"category": [{
"id": 43,
"title": "XXX",
"titleFr": "YYY"
}]
}]
I would like to be able to find all the Unique Category Ids, then create a new Data Structure (array) by Grouping by the Category...ie
Category 27- AAA
This is a Story title
Category 43 - XXX
This is a Story title
This is a Story title 2
I have the below code, which I can use so far to traverse and get the Categories
$.each(data, function (i, ob) {
$.each(ob, function (ind, obj) {
if (ind === "category") {
Categories.push(this);
}
});
});
console.log(Categories);
I feel like the above would be highly inefficient, plus it doesn't account for if the Category already exists.
Secondly, I think I would then need to go back over the entire list again, look for the Category.Id, create a new obj when found, then add to a new list?
I feel like there's a lot of looping here and using $.each would be very inefficient.
Any help would be appreciated.