zoukankan      html  css  js  c++  java
  • 记录一个排序方法

    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];
      }, []);
    }
    
    
  • 相关阅读:
    2017 湖南省赛 K Football Training Camp
    一些相似单词的区别之处
    LeetCode301. Remove Invalid Parentheses
    算法刷题细节点总结
    LeetCode765. Couples Holding Hands
    LeetCode741. Cherry Pickup
    LeetCode312. Burst Balloons
    LeetCode679. 24 Game
    LeetCode862. Shortest Subarray with Sum at Least K
    LeetCode818. Race Car
  • 原文地址:https://www.cnblogs.com/yzyh/p/15019748.html
Copyright © 2011-2022 走看看