前言
sql语句和pyhton中都有groupby的操作,处理数据来非常方便。而js对group不支持
于是想着自己写一个
单个字段group
单个字段聚合的话非常简单,
const arr = [
{name: '小孙', age: 18, score: 60, weight: 60},
{name: '小王', age: 19, score: 70, weight: 55},
{name: '小李', age: 18, score: 60, weight: 70},
{name: '小刘', age: 20, score: 70, weight: 65},
{name: '小赵', age: 18, score: 60, weight: 60},
{name: '小钱', age: 19, score: 70, weight: 55},
{name: '小周', age: 20, score: 60, weight: 50},
];
const group_signal = (data, key) => {
return data.reduce(function(prev, cur) {
(prev[cur[key]] = prev[cur[key]] || []).push(cur);
return prev;
}, {});
};
console.log(group_signal(arr, 'age'));
object: {18: Array(3), 19: Array(2), 20: Array(2)}
18: Array(3)
0: {name: "小孙", age: 18, score: 60, weight: 60}
1: {name: "小李", age: 18, score: 60, weight: 70}
2: {name: "小赵", age: 18, score: 60, weight: 60}
19: Array(2)
0: {name: "小王", age: 19, score: 70, weight: 55}
1: {name: "小钱", age: 19, score: 70, weight: 55}
20: Array(2)
0: {name: "小刘", age: 20, score: 70, weight: 65}
1: {name: "小周", age: 20, score: 60, weight: 50}
多个字段group
不光有单个字段聚合,也有多个字段聚合。比如我想找出 age 和 score 相同的。
多个字段聚合相对复杂些,首先要找到要聚合的字段,并且去掉重复的值。这里用到的函数比较多,我们将它封装成一个类
class GroupMultiple {
//data是聚合的数据, name是聚合的字段
constructor (data, name){
this.data = data;
this.name = name;
}
}
然后定义一个getKeys方法取出聚合后的下标。例如按照 age 和 score 聚合 将 age 和 score 不同的值取出来放入一个数组。
class GroupMultiple {
//data是聚合的数据, name是聚合的字段
constructor (data, name){
this.data = data;
this.name = name;
}
getKeys(data, names){
let _this = this;
return data.reduce(function (prev, cur) {
//首先拿到name对应的值, 将key组成 {key: value}形式
var key = _this.getObject(cur, names);
//判断数组里是否已经含有了key,有的话就跳过,否则把key放入到数组里
//把判断方法封装成一个函数 isHasObject
if(!_this.isHasObject(prev, key)) {
prev.push(key);
}
return prev
}, [])
}
}
getObject是将name转换为对应的对象{key:value},拿到key之后,判断数组里是否已经含有了key,有的话就跳过,否则把key放入到数组里。
class GroupMultiple {
//data是聚合的数据, name是聚合的字段
constructor (data, name){
this.data = data;
this.name = name;
}
//获取key
getKeys(data, names){
let _this = this;
return data.reduce(function (prev, cur) {
//首先拿到name对应的值, 将key组成 {key: value}形式
var key = _this.getObject(cur, names);
//判断数组里是否已经含有了key,有的话就跳过,否则把key放入到数组里
//把判断方法封装成一个函数 isHasObject
if(!_this.isHasObject(prev, key)) {
prev.push(key);
}
return prev
}, [])
}
getObject(obj, keys=this.name) {
let aa = {};
for (var i = 0, length = keys.length; i < length; i++) {
aa[keys[i]] = obj[keys[i]]
}
return aa
}
//判断数组中是否有了某个对象
isHasObject(prev, keys){
//数组长度为0的时候直接返回
if (prev.length === 0) return false;
var _this = this;
//判断数组中的每一项是否与keys相等, 这里用到数组的find方法,当数组的每一项与keys都不想等的时候(也就是数组中没有这个对象)返回undefined,
//isObjectValueEqual 是判断两个对象是否相等的方法
return prev.find(function (item) {
return _this.isObjectValueEqual(item, keys)
})
}
//判断两个对象是否相等
isObjectValueEqual(obj1, obj2) {
// 只有两个对象的key和value完全相等时这两个对象才相等
var _this = this;
var keys1 = Object.keys(obj1);
var keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) {
return false;
}
for (var i = 0; i < keys1.length; i++) {
var key = keys1[i];
if (obj1[key] !== obj2[key]) {
return false;
}
}
return true;
}
}
拿到对应的key之后,需要把每一个key在数组中对应的值取出来
class GroupMultiple {
...
//返回一个数组中包含某个对象的元素
hasObjectData(data, obj){
return data.filter(function (item) {
return Object.keys(obj).reduce(function(pr, cr){
if (pr){
return obj[cr] === item[cr]
} else {
return false
}
}, true)
});
}
}
然后将结果返回
class GroupMultiple {
...
//返回一个数组中包含某个对象的元素
hasObjectData(data, obj){
return data.filter(function (item) {
return Object.keys(obj).reduce(function(pr, cr){
if (pr){
return obj[cr] === item[cr]
} else {
return false
}
}, true)
});
}
//返回结果
init(){
let _this = this;
//拿到keys
let keys = _this.getKeys(_this.data, this.name);
return keys.map(function(item) {
//拿到keys对应的值
let vals = _this.hasObjectData(_this.data, item);
return {
key: item,
value: vals
};
});
}
}
var group_result = new GroupMultiple (arr, ['age', 'score']);
console.log(group_result .init())
[{…}, {…}, {…}, {…}]
0:
key: {age: 18, score: 60}
value: Array(3)
0: {name: "小孙", age: 18, score: 60, weight: 60}
1: {name: "小李", age: 18, score: 60, weight: 70}
2: {name: "小赵", age: 18, score: 60, weight: 60}
1:
key: {age: 19, score: 70}
value: Array(2)
0: {name: "小王", age: 19, score: 70, weight: 55}
1: {name: "小钱", age: 19, score: 70, weight: 55}
2:
key: {age: 20, score: 70}
value: Array(1)
0: {name: "小刘", age: 20, score: 70, weight: 65}
3:
key: {age: 20, score: 60}
value: Array(1)
0: {name: "小周", age: 20, score: 60, weight: 50}
到这里多个字段的聚合算是完成了。但是有个问题,要是我想聚合之后做其他的操作呢?比如说求和,求平均等。
group之后对数据的再处理
例如求和:
增加一个sum方法,并且在增加要操作的对象operating,修改init方法
class GroupMultiple {
constructor (data, name, operating=null){
this.data = data;
this.name = name;
this.operating = operating;
}
...
//返回结果
init(){
let _this = this;
//拿到keys
let keys = _this.getKeys(_this.data, this.name);
return keys.map(function(item) {
//拿到keys对应的值
let vals = _this.hasObjectData(_this.data, item);
if (_this.operating){
Object.keys(_this.operating).forEach(opitem => {
let ret = _this[_this.operating[opitem]](opitem, vals);
vals.forEach(val => {
val[opitem] = ret
})
})
}
return {
key: item,
value: vals
};
});
}
sum(key, data){
//key是要求和的字段, data是要求和的数组
return data.reduce(function (prev, cur) {
return prev + cur[key]
}, 0)
}
}
//对 age 和 score聚合, 然后对weight求和
var sum_group = new GroupMultiple(arr, ['age', 'score'], {weight: 'sum'});
console.log(sum_group.init())
[{…}, {…}, {…}, {…}]
0:
key: {age: 18, score: 60}
value: Array(3)
0: {name: "小孙", age: 18, score: 60, weight: 190}
1: {name: "小李", age: 18, score: 60, weight: 190}
2: {name: "小赵", age: 18, score: 60, weight: 190}
1:
key: {age: 19, score: 70}
value: Array(2)
0: {name: "小王", age: 19, score: 70, weight: 110}
1: {name: "小钱", age: 19, score: 70, weight: 110}
2:
key: {age: 20, score: 70}
value: Array(1)
0: {name: "小刘", age: 20, score: 70, weight: 65}
3:
key: {age: 20, score: 60}
value: Array(1)
0: {name: "小周", age: 20, score: 60, weight: 50}
可以看到每一项的weight都成为该分组中weight的和了。
同理还可以增加 mean(),max(),min(),first(),end()等。不过不建议这么做,处理数据最好还是在后台处理,js毕竟对数据处理的支持不到位。
源代码: https://github.com/controZheng/work