我试图在jQuery中通过Id对我的数组进行分组,以下是我的代码的详细信息以及我想要实现的目标。
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"
每个记录中都有一些categoryId,对于某些记录可能是相同的,我需要根据类别id对数组进行分组并创建一个新的json数组。
My Required输出如下:
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"
}]
}];
请帮忙!
答案 0 :(得分:0)
如果您正在处理一组对象 - 请使用Array.forEach
和Object.keys
函数使用以下方法:
var result = [
{
Description: "Testing Desc1",
CategoryName: "Fruit",
CategoryId: 1,
Title: "Data1"},
{
Description: "Testing Desc2",
CategoryName: "Veg",
CategoryId: 2,
Title: "Data2"},
{
Description: "Testing Desc3",
CategoryName: "Fruit",
CategoryId: 1,
Title: "Data3"},
{
Description: "Testing Desc4",
CategoryName: "Grains",
CategoryId: 3,
Title: "Data4"},
{
Description: "Testing Desc5",
CategoryName: "Grains",
CategoryId: 3,
Title: "Data5"
}
],
groups = {};
result.forEach(function (o){
var dataObj = {Title: o.Title, Description: o.Description};
if (groups[o.CategoryId]) {
groups[o.CategoryId]['Data'].push(dataObj);
} else {
groups[o.CategoryId] = {Category: o.CategoryName, Data: [dataObj]};
}
});
var data = Object.keys(groups).map(function(k) { return groups[k]; });
console.log(JSON.stringify(data, 0, 4));

答案 1 :(得分:0)
我的提案基于Array sort和Array reduce:
var data = [{
Description: "Testing Desc1",
CategoryName: "Fruit",
CategoryId: 1,
Title: "Data1"
}, {
Description: "Testing Desc2",
CategoryName: "Veg",
CategoryId: 2,
Title: "Data2"
}, {
Description: "Testing Desc3",
CategoryName: "Fruit",
CategoryId: 1,
Title: "Data3"
}, {
Description: "Testing Desc4",
CategoryName: "Grains",
CategoryId: 3,
Title: "Data4"
}, {
Description: "Testing Desc5",
CategoryName: "Grains",
CategoryId: 3,
Title: "Data5"
}];
var result = data.sort((a, b) => {return a.CategoryName.localeCompare(b.CategoryName);}).reduce(function(a,b,c) {
if (c == 1) {
if (a.CategoryName == b.CategoryName) {
return [{
Category: a.CategoryName,
Data: [{
Title: a.Title,
Description: a.Description
}, {
Title: b.Title,
Description: b.Description
}]
}];
} else {
return [{
Category: a.CategoryName,
Data: [{
Title: a.Title,
Description: a.Description
}]
}, {
Category: b.CategoryName,
Data: [{
Title: b.Title,
Description: b.Description
}]
}];
}
} else {
if (a[a.length - 1].Category == b.CategoryName) {
a[a.length - 1].Data.push({
Title: b.Title,
Description: b.Description
});
} else {
a.push({
Category: b.CategoryName,
Data: [{
Title: b.Title,
Description: b.Description
}]
});
}
return a;
}
});
console.log(JSON.stringify(result, null, 4));