1、对数组分组,根据某一个字段分组
groupBy(array, f) {
const groups = {};
array.forEach(function (o) {
const group = JSON.stringify(f(o));
groups[group] = groups[group] || [];
groups[group].push(o);
});
return Object.keys(groups).map(function (group) { return groups[group]; });
}
let resultList = groupBy(list, (item) =>{ return [item.name]; });
2、排序
function compare(property,desc) {
![](/qrcode.jpg)
return function (a, b) {
var value1 = a[property];
var value2 = b[property];
if(desc==true){
// 升序排列
return value1 - value2;
}else{
// 降序排列
return value2 - value1;
}
}
}
list.sort(compare("index",true));