僅作為學(xué)習(xí)記錄下:
//分組欠动,一維數(shù)組變?yōu)槎S數(shù)組
function?groupBy(?array?,?f?)?{
????let?groups?=?{};
????array.forEach(item=>{
????????let?group?=?JSON.stringify(f(item));
????????groups[group]?=?groups[group]?||?[];?//?groups['John']=groups['Jhone']||[];
????????groups[group].push(item);
????});
????console.log('groups:',groups);
????console.log('======================')
????return?Object.keys(groups).map(key=>?{
????????return?groups[key];
????});
}
let?list?=?[
????{"name":?"John","Average":15,"High":10},
????{"name":?"Jane","Average":16,"High":92},
????{"name":?"Jane","Average":17,"High":45},
????{"name":?"John","Average":18,"High":87},
????{"name":?"Jane","Average":15,"High":10},
????{"name":?"John","Average":16,"High":87},
????{"name":?"John","Average":17,"High":45},
????{"name":?"Jane","Average":18,"High":92}
];
let?sorted?=?groupBy(list,?(item)=>{
????return?[item.name]
});
console.log(sorted);