groupBy = function ( array , f ) {
var groups = {};
array.forEach( function( o ) {
var group = JSON.stringify( f(o) );
groups[group] = groups[group] || [];
groups[group].push( o );
});
return Object.keys(groups).map( function( group ) {
return groups[group];
});
}
var newFetchData = groupBy(fetchData, function(item){
return [要分组的属性];
});
09
25
JS 数组根据属性实现类似sql的group by 分组
分享到: