js分组方法

1.分组-得到一个二维数组

/** array-要分组的对象数组 name-分类标识 */
export const groupBy = (array: any[], name: string) => {
  const groups = {};
  array.forEach(function (o) {
    const group = JSON.stringify(o[name]);
    groups[group] = groups[group] || [];
    groups[group].push(o);
  });
  return Object.values(groups);
};

const name = 'source';
const links = [
  { source: 'test1', target: 'test1', value: 10 },
  { source: 'test1', target: 'test2', value: 30 },
  { source: 'test1', target: 'test3', value: 40 },
  { source: 'test2', target: 'test4', value: 20 },
  { source: 'test2', target: 'test4', value: 20 },
  { source: 'test2', target: 'test4', value: 20 },
];
const groupData = groupBy(links, name);
console.log(groupData);
// [
//   [
//     { source: 'test1', target: 'test1', value: 10 },
//     { source: 'test1', target: 'test2', value: 30 },
//     { source: 'test1', target: 'test3', value: 40 },
//   ],
//   [
//     { source: 'test2', target: 'test4', value: 20 },
//     { source: 'test2', target: 'test4', value: 20 },
//     { source: 'test2', target: 'test4', value: 20 },
//   ]
// ]

2.分组-得到一个数组对象

export const groupBy = (array: any[], name: string) => {
  const groups = {};
  array.forEach(function (o) {
    const group = JSON.stringify(o[name]);
    groups[group] = groups[group] || [];
    groups[group].push(o);
  });
  return groups;
};

const name = 'source';
const links = [
  { source: 'test1', target: 'test1', value: 10 },
  { source: 'test1', target: 'test2', value: 30 },
  { source: 'test1', target: 'test3', value: 40 },
  { source: 'test2', target: 'test4', value: 20 },
  { source: 'test2', target: 'test4', value: 20 },
  { source: 'test2', target: 'test4', value: 20 },
];
const groupData = groupBy(links, name);
console.log(groupData);
// {
//  'test1':[
//      { source: 'test1', target: 'test1', value: 10 },
//      { source: 'test1', target: 'test2', value: 30 },
//      { source: 'test1', target: 'test3', value: 40 },
//     ],
//  'test2':[
//      { source: 'test2', target: 'test4', value: 20 },
//      { source: 'test2', target: 'test4', value: 20 },
//      { source: 'test2', target: 'test4', value: 20 },
//     ]
// }

1 函数groupBy有两个形参,一为对象数组,二为指定分类方式的key;
2 groupBy函数内,先创建一个空对象;
3 然后forEach遍历对象数组,遍历时要执行的函数中只有一个形参o(数组中的每个元素);
4 由于下面函数调用是想用source来分组,因此let group = JSON.stringify( f(o) ),相当于先获取到对象数组list中的source属性对应的属性值并放入数组中:[‘test1’],然后再将属性值转换为json字符串:“[‘test1’]”;
5 groups[group] = groups[group] || [],在js中对象也是关联数组,因此这里相当于做了两件事,一是把group作为groups的key,二是将对应的value初始化,第一次执行为空数组,循环执行时找到相同的source时保持不变;
6 groups[group].push( o ),这句相当于把list中每个对象压入数组中作为value;
7 Object.values(groups)是取出groups对象中的所有values,返回分好了组的二维数组

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值