js实现数据统计分类

        let msg = [{ name: "张意",address: "北京"},
           { name: "李尔",job: "前端开发",salary: "13k"},
           { job: "python开发",salary: "15k"},
           { name: "王新",job: "java开发",salary: "20k",address: "上海"},
           { job: "ios开发", salary: "20k",address: "深圳"}]
        let newMsg = {}
        let allKey = []
        //把所有key保存到一个空数组中
        msg.map((item, index) => {
            for (let i in item) {
                if (allKey.indexOf(i) == -1) {
                    allKey.push(i)
                }
            }
        })
        //新数组中保存不同key的可能值
        allKey.map((item, index) => {
            newMsg[item] = []
        })
        for (let j in newMsg) {
            msg.map((item, index) => {
                for (let i in item) {
                    if (j == i) {
                        newMsg[j].push(item[i])
                    }
                }
            })
        }
        console.log(newMsg)

打印结果如下

js实现数据统计分类_第1张图片 打印结果中的key可通过newMsg[]获取,例newMsg[‘name’]

 

你可能感兴趣的:(js实现数据统计分类)