• 对象型数组按某个字段分类


       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');
  • 相关阅读:
    react之setState面试题
    react之setState异步和同步问题
    react关于setState的使用
    antd框架tree树动态插入,解决新版Antd无法使用TreeNodes问题
    postman测试接口时参数为数组时怎么测试?
    你不知道的react更新状态this.setState细节点
    浅谈md5加密技术
    浅谈纯文本&&富文本&&Markdown区别
    【LeetCode】43. Multiply Strings
    【LeetCode】44. Wildcard Matching (2 solutions)
  • 原文地址:https://www.cnblogs.com/ouyangfeifei/p/15030418.html
Copyright © 2020-2023  润新知