记录一个排序方法

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];
  }, []);
}

上一篇:有道词典 Kubuntu20.04


下一篇:量子计算或攻破区块链的两大命门,比特币危险了?