I am trying to group my array by Id in jQuery, Here are the details of my code and what I am trying to achieve.
var result = Array[5] // array of 5 records
Array[0]:
Description: "Testing Desc1"
CategoryName: "Fruit"
CategoryId : 1
Title: "Data1"
Array[1]:
Description: "Testing Desc2"
CategoryName: "Veg"
CategoryId : 2
Title: "Data2"
Array[2]:
Description: "Testing Desc3"
CategoryName: "Fruit"
CategoryId : 1
Title: "Data3"
Array[3]:
Description: "Testing Desc4"
CategoryName: "Grains"
CategoryId : 3
Title: "Data4"
Array[4]:
Description: "Testing Desc5"
CategoryName: "Grains"
CategoryId : 3
Title: "Data5"
Each record has some categoryId in it which can be same for some of records, I need to group array on basis of category id and create a new json array.
My Required output is like this:
var data = [{
Category: "Fruit",
Data: [{
Title: "Data1",
Description: "Testing Desc1"
}, {
Title: "Data3",
Description: "Testing Desc3"
}]
}, {
Category: "Veg",
Data: [{
Title: "Data2",
Description: "Testing Desc2"
}]
}, {
Category: "Grains",
Data: [{
Title: "Data4",
Description: "Testing Desc4"
}, {
Title: "Data5",
Description: "Testing Desc5"
}]
}];
Please help!
CategoryNamefruit is data1 and data3, and in the result fruit is data1 and data2, also veg is data2 and then turns into data3...and same with grains not lining up...? Is this an intended pattern?