zoukankan      html  css  js  c++  java
  • 对象型数组按某个字段分类

       function groupByKey(list, key) {
                console.log(list);
                let tmpKeyList = list.map(o => o[key]);
                let keyList = [...new Set(tmpKeyList)];
                console.log(keyList); //一共有多少种分类
                let result = [];
                keyList.map(o => {
                    let data = [];
                    for (let i = 0, len = list.length; i < len; i++) {
                        if (list[i][key] == o) {
                            data.push(list[i].CNT);
                        }
                    }
                    result.push({
                        name: o,
                        data
                    });
                });
                console.log(result);
            }
    let list = [{
                APP_ID: "APP001",
                CNT: 0,
                STAT_DATE: "06-20"
            }, {
                APP_ID: "APP002",
                CNT: 0,
                STAT_DATE: "06-20"
            }, {
                APP_ID: "APP001",
                CNT: 0,
                STAT_DATE: "06-21"
            }, {
                APP_ID: "APP002",
                CNT: 0,
                STAT_DATE: "06-21"
            }];
    groupByKey(list, 'APP_ID');
  • 相关阅读:
    [LeetCode] Substring with Concatenation of All Words 解题报告
    [LeetCode] Symmetric Tree 解题报告
    [LeetCode] Unique Paths II 解题报告
    [LeetCode] Triangle 解题报告
    [LeetCode] Trapping Rain Water 解题报告
    [LeetCode] Text Justification 解题报告
    [LeetCode] Valid Sudoku 解题报告
    [LeetCode] Valid Parentheses 解题报告
    C++文件的批处理
    C++中两个类中互相包含对方对象的指针问题
  • 原文地址:https://www.cnblogs.com/ouyangfeifei/p/15030418.html
Copyright © 2011-2022 走看看