其他分享
首页 > 其他分享> > 记录一个排序方法

记录一个排序方法

作者:互联网

var list = [
  {
    name: "1",
    age: "2",
    chi: "2",
  },
  {
    name: "2",
    age: "3",
    chi: "4",
  },
  {
    name: "2",
    age: "4",
    chi: "8",
  },
  {
    name: "1",
    age: "2",
    chi: "2",
  },
  {
    name: "1",
    age: "8",
    chi: "4",
  },
  {
    name: "2",
    age: "7",
    chi: "3",
  },
  {
    name: "2",
    age: "5",
    chi: "7",
  },
  {
    name: "2",
    age: "3",
    chi: "2",
  },
  {
    name: "1",
    age: "6",
    chi: "2",
  },
];
const skuSort = {
  name: ["9", "8", "7", "6", "5", "4", "3", "2", "1"],
  age: ["9", "8", "7", "6", "5", "4", "3", "2", "1"],
  chi: ["1", "2", "3", "4", "5", "6", "7", "8", "9"],
};
const attributeArr = [
  {
    propertyId: "name",
  },
  {
    propertyId: "age",
  },
  {
    propertyId: "chi",
  },
];

var c = sortFn(skuSort, list, 0);
c;

function sortFn(skuSort, list, index) {
  if (index >= attributeArr.length) return list;
  const propertyName = attributeArr[index].propertyId;
  const sortArr = skuSort[propertyName];

  return sortArr.reduce((arrs, item) => {
    // 筛序出当前排序的数据
    let curRow = list.filter((v) => v[propertyName] === item);

    curRow = sortFn(skuSort, curRow, index + 1);
    // 分别对数据进行合并单元格数量的处理
    curRow = curRow.map((curt, index) => {
      if (index === 0) {
        curt[`${propertyName}_rowSpan`] = curRow.length;
      } else {
        curt[`${propertyName}_rowSpan`] = 0;
      }
      return curt;
    });
    // 合并数据
    return [...arrs, ...curRow];
  }, []);
}

标签:skuSort,index,name,记录,chi,age,curRow,排序,方法
来源: https://www.cnblogs.com/yzyh/p/15019748.html