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

posted on 2021-07-16 13:57  佑之以航  阅读(55)  评论(0编辑  收藏  举报