其他分享
首页 > 其他分享> > 分组排序

分组排序

作者:互联网

 

 

 

/**
 * 分组排序
 */
const sortByKey = (array: object[], key: string) => {
  return array.reduce((total, next) => {
    const index = total.findIndex((item, index, self) => {
      return item[key] === next[key];
    });
    return index === -1
      ? total.push(next) && total
      : total.splice(index, 0, next) && total;
  }, []);
};

 

 

/**
 * 计算合并
 */
const merge = (list: any[], key: string): any => {
  const len = list.length;
  const rowsSpan = {};
  let step = 0;
  for (let i = 0; i < len - 1; i++) {
    if (list[i][key] === list[i + 1][key]) {
      if (!rowsSpan[step]) {
        rowsSpan[step] = 1;
      }
      rowsSpan[step]++;
    } else {
      step = i + 1;
      rowsSpan[step] = 1;
    }
  }
  return rowsSpan;
};

 

标签:排序,const,list,step,rowsSpan,分组,key,total
来源: https://www.cnblogs.com/yeminglong/p/16560206.html