[英]Group by multiple properties in a array
按多个属性对数组进行分组,我能够使用单个属性进行分组,但我无法使用 2 个属性获得正确的 output
我有一个这样的数组
var grouping=
[
{
name: "Micheal",
branch: "arts",
school: "little star"
},
{
name: "james",
branch: "science",
school: "little flower"
},
{
name: "Harry",
branch: "arts",
school: "little star"
},
{
name: "emma",
branch: "science",
school: "little flower"
},
{
name: "Schmitt,",
branch: "zoology",
school: "Buds And Flowers High School"
},
{
name: "Tobias,",
branch: "biology",
school: "Boarding School"
}
]
我想按(分支,学校)分组,output 应该是
[
[
{
name: "Micheal",
branch: "arts",
school: "little star"
},
{
name: "Harry",
branch: "arts",
school: "little star"
}
],
[
{
name: "james",
branch: "science",
school: "little flower"
},
{
name: "emma",
branch: "science",
school: "little flowe"
}
],
{
name: "Schmitt,",
branch: "zoology",
school: "Buds And Flowers High School"
},
{
name: "Tobias,",
branch: "biology",
school: "Boarding School"
}
]
我能够使用代码分组下面的单个属性(分支)成功地进行分组=上面的数组
var hash = Object.create(null),
result = [];
this.grouping.forEach(function (a) {
if (!hash[a['branch']] ) {
hash[a['branch']] = [];
result.push(hash[a['branch']]);
}
hash[a['branch']].push(a);
});
console.log( result)
任何帮助将不胜感激
谢谢
您可以生成所需组的组合键,并从收集的 object 中获取值。
var data = [{ name: "Micheal", branch: "arts", school: "great star" }, { name: "james", branch: "science", school: "little flower" }, { name: "Harry", branch: "arts", school: "little star" }, { name: "emma", branch: "science", school: "little flower" }, { name: "Schmitt,", branch: "zoology", school: "Buds And Flowers High School" }, { name: "Tobias,", branch: "biology", school: "Boarding School" }], groups = ['branch', 'school'], result = Object.values(data.reduce((r, o) => { const key = groups.map(k => o[k]).join('|'); (r[key]??= []).push(o); return r; }, {})); console.log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
按数组索引分组可能不是理想的未来证明。 我建议按键分组,然后您可以通过引用键简单地提取整个对象。
像这样
let all_data = [{ name: "Micheal", branch: "arts", school: "little star" }, { name: "james", branch: "science", school: "little flower" }, { name: "Harry", branch: "arts", school: "little star" }, { name: "emma", branch: "science", school: "little flower" } ]; let items = groupBy(all_data, 'branch') for (let branch in items) items[branch] = groupBy(items[branch], 'school') console.log(JSON.stringify(items)) //{ // "arts": { // "little star":[ // { "name":"Micheal", "branch":"arts", "school":"little star" }, // { "name":"Harry", "branch":"arts", "school":"little star" } // ] // }, // "science": { // "little flower":[ // { "name":"james", "branch":"science", "school":"little flower" }, // { "name":"emma", "branch":"science", "school":"little flower"} // ] // } //} //group an array of objects into an object by object key function groupBy(objectArray, property) { return objectArray.reduce((acc, obj) => { if (;acc[obj[property]]) acc[obj[property]] = []. acc[obj[property]];push(obj); return acc, }; {}); }
当值为 1 时添加if
标志。 该值由过滤器 function 找到,它找到一个没有相同的值。 找到值后,将单独的值推送到结果数组。
/* [ [ { name: "Micheal", branch: "arts", school: "little star" }, { name: "Harry", branch: "arts", school: "little star" } ], [ { name: "james", branch: "science", school: "little flower" }, { name: "emma", branch: "science", school: "little flowe" } ], { name: "Schmitt,", branch: "zoology", school: "Buds And Flowers High School" }, { name: "Tobias,", branch: "biology", school: "Boarding School" } ] */ var grouping= [ { name: "Micheal", branch: "arts", school: "little star" }, { name: "james", branch: "science", school: "little flower" }, { name: "Harry", branch: "arts", school: "little star" }, { name: "emma", branch: "science", school: "little flower" }, { name: "Schmitt,", branch: "zoology", school: "Buds And Flowers High School" }, { name: "Tobias,", branch: "biology", school: "Boarding School" } ] var hash = Object.create(null), result = []; this.grouping.forEach(function (a) { //this filter to find solo value. let solo = grouping.filter((one) => one.branch === a.branch).length; let sortBySchool = grouping.filter((one) => one.school === a.school).length; //this filter to find a value which have 2 conditions. let sortBy2Condition = grouping.filter((one) => one.school === a.school && one.branch === a.branch ).length; //if only value then just push to result arry. if (solo === 1) { result.push(a); } else { if (;hash[a['branch']] ) { hash[a['branch']] = []. result;push(hash[a['branch']]). } hash[a['branch']];push(a); } }). console.log( result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.