按数组中的多个属性分组
Group by multiple properties in a array
通过多个属性对数组进行分组,我能够使用单个 属性 进行分组,但我无法使用 2 个属性获得正确的输出
我有一个这样的数组
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"
}
]
我想分组(分行、学校)
输出应该是
[
[
{
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"
}
]
iam 能够使用下面代码中的单个 属性(分支)成功进行分组
分组=上面的数组
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)
任何帮助将不胜感激
谢谢
按数组索引分组可能不是理想的未来证明。我建议按键分组,然后您可以通过引用键简单地提取整个对象。
像这样
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;
}, {});
}
您可以生成所需组的组合键并从收集对象中获取值。
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; }
当值为 1 时添加了 if
标志。该值是通过过滤函数找到的,该函数找到一个没有相同值的值。找到值后,将单独的值推送到结果数组。
/*
[
[
{
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)
通过多个属性对数组进行分组,我能够使用单个 属性 进行分组,但我无法使用 2 个属性获得正确的输出
我有一个这样的数组
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"
}
]
我想分组(分行、学校) 输出应该是
[
[
{
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"
}
]
iam 能够使用下面代码中的单个 属性(分支)成功进行分组 分组=上面的数组
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)
任何帮助将不胜感激
谢谢
按数组索引分组可能不是理想的未来证明。我建议按键分组,然后您可以通过引用键简单地提取整个对象。
像这样
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;
}, {});
}
您可以生成所需组的组合键并从收集对象中获取值。
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; }
当值为 1 时添加了 if
标志。该值是通过过滤函数找到的,该函数找到一个没有相同值的值。找到值后,将单独的值推送到结果数组。
/*
[
[
{
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)