吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1179|回复: 12
收起左侧

[求助] JS数组对象统计的方法

[复制链接]
cqwcns 发表于 2020-7-29 08:07


朕有一个数组对象,结构是这样的:

[JavaScript] 纯文本查看 复制代码
[
{id: "2a0398605f1c160f0072498e5f4942e6", region: "茂名"},
{id: "9bf625a55f1c1616006bed846372e6e3", region: "电白"},
{id: "f47f682c5f1c163f008839ef2a343440", region: "茂名"},
{id: "3d23c0a05f1c1681008a5b3a0b8bb446", region: "茂名"},
{id: "f47f682c5f1c168c00883c273a954b1e", region: "茂南"},
{id: "08e51e265f1c16910070c8117bbe476c", region: "茂名"},
{id: "15d399db5f1c16ef0086f16b2a83a752", region: "茂名"},
{id: "08e51e265f1c1b340070e7675a7c02df", region: "茂名"},
{id: "9bf625a55f1c1b40006c0e721d7a7690", region: "高州"},
{id: "9bf625a55f1c1b44006c0e916570035a", region: "高州"},
{id: "3d23c0a05f1c1b52008a840d3b62a411", region: "高州"},
{id: "2a0398605f1c1b5600726d490bf434c9", region: "化州"},
]


朕希望对region进行统计,输出这样的结果:
[JavaScript] 纯文本查看 复制代码
{茂名:6,电白:1,茂南:1,高州:3,化州:1}


怎样统计比较科学?诸王有何高见,请指教,谢谢

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Gaho2002 发表于 2020-7-29 08:21
非程序员看不懂
Ricardo_M_Zh 发表于 2020-7-29 08:33
本帖最后由 Ricardo_M_Zh 于 2020-7-29 08:37 编辑

    arrS() {
      let data = {};
      this.arr.forEach((item) => {
        if (data[item.region]) {
          data[item.region]++;
        } else {
          data[item.region] = 0;
        }
      });
      console.log(data);
    },
ssd1997 发表于 2020-7-29 08:33
var arr = [
{id: "2a0398605f1c160f0072498e5f4942e6", region: "茂名"},
{id: "9bf625a55f1c1616006bed846372e6e3", region: "电白"},
{id: "f47f682c5f1c163f008839ef2a343440", region: "茂名"},
{id: "3d23c0a05f1c1681008a5b3a0b8bb446", region: "茂名"},
{id: "f47f682c5f1c168c00883c273a954b1e", region: "茂南"},
{id: "08e51e265f1c16910070c8117bbe476c", region: "茂名"},
{id: "15d399db5f1c16ef0086f16b2a83a752", region: "茂名"},
{id: "08e51e265f1c1b340070e7675a7c02df", region: "茂名"},
{id: "9bf625a55f1c1b40006c0e721d7a7690", region: "高州"},
{id: "9bf625a55f1c1b44006c0e916570035a", region: "高州"},
{id: "3d23c0a05f1c1b52008a840d3b62a411", region: "高州"},
{id: "2a0398605f1c1b5600726d490bf434c9", region: "化州"},
]
var ob = {}
for(const item of arr) {
  if(!ob[item.region]) {
    ob[item.region]=0
  }
  ob[item.region]++;
}
console.log(ob)

命名随便命的
ssd1997 发表于 2020-7-29 08:35
ssd1997 发表于 2020-7-29 08:33
var arr = [
{id: "2a0398605f1c160f0072498e5f4942e6", region: "茂名"},
{id: "9bf625a55f1c1616006bed ...

如果要达到你说的那种输出  {"茂名":6,"电白":1,"茂南":1,"高州":3,"化州":1}
就把最后一行 console.log(ob) 改为 console.log(JSON.stringify(ob))

免费评分

参与人数 1吾爱币 +1 收起 理由
cqwcns + 1 热心回复!

查看全部评分

池中金麟 发表于 2020-7-29 08:45
var a = [{
        id: "2a0398605f1c160f0072498e5f4942e6",
        region: "茂名"
    },
    {
        id: "9bf625a55f1c1616006bed846372e6e3",
        region: "电白"
    },
    {
        id: "f47f682c5f1c163f008839ef2a343440",
        region: "茂名"
    },
    {
        id: "3d23c0a05f1c1681008a5b3a0b8bb446",
        region: "茂名"
    },
    {
        id: "f47f682c5f1c168c00883c273a954b1e",
        region: "茂南"
    },
    {
        id: "08e51e265f1c16910070c8117bbe476c",
        region: "茂名"
    },
    {
        id: "15d399db5f1c16ef0086f16b2a83a752",
        region: "茂名"
    },
    {
        id: "08e51e265f1c1b340070e7675a7c02df",
        region: "茂名"
    },
    {
        id: "9bf625a55f1c1b40006c0e721d7a7690",
        region: "高州"
    },
    {
        id: "9bf625a55f1c1b44006c0e916570035a",
        region: "高州"
    },
    {
        id: "3d23c0a05f1c1b52008a840d3b62a411",
        region: "高州"
    },
    {
        id: "2a0398605f1c1b5600726d490bf434c9",
        region: "化州"
    },
];
var result = {};
a.forEach(function(ele,i){
    if(result[ele.region]==undefined){
        result[ele.region]=1;
    }else{
        result[ele.region]++;
    }
});
console.log(result);
 楼主| cqwcns 发表于 2020-7-29 08:52
谢谢各位热心回复
田多 发表于 2020-7-29 08:58
js初学,学习了上面几个师兄的写法
clyzhi 发表于 2020-7-29 09:03
给出两种输出结果,第二种更符合题意
[JavaScript] 纯文本查看 复制代码
let list = [
{id: "2a0398605f1c160f0072498e5f4942e6", region: "茂名"},
{id: "9bf625a55f1c1616006bed846372e6e3", region: "电白"},
{id: "f47f682c5f1c163f008839ef2a343440", region: "茂名"},
{id: "3d23c0a05f1c1681008a5b3a0b8bb446", region: "茂名"},
{id: "f47f682c5f1c168c00883c273a954b1e", region: "茂南"},
{id: "08e51e265f1c16910070c8117bbe476c", region: "茂名"},
{id: "15d399db5f1c16ef0086f16b2a83a752", region: "茂名"},
{id: "08e51e265f1c1b340070e7675a7c02df", region: "茂名"},
{id: "9bf625a55f1c1b40006c0e721d7a7690", region: "高州"},
{id: "9bf625a55f1c1b44006c0e916570035a", region: "高州"},
{id: "3d23c0a05f1c1b52008a840d3b62a411", region: "高州"},
{id: "2a0398605f1c1b5600726d490bf434c9", region: "化州"},
];

let tempObj = {}
list.forEach(a=>{
    tempObj[a.region] = (tempObj[a.region]||0) + 1
})
console.log(tempObj)
let printSting = '{',i=0
for (let key in tempObj) {
    printSting += `${key}:${tempObj[key]}${i++?",":""}`
}
let id = printSting.length-1;

console.log(printSting.substring(0,printSting.length-1)+'}')
qujf 发表于 2020-7-29 09:03
var data = [
{id: "2a0398605f1c160f0072498e5f4942e6", region: "茂名"},
{id: "9bf625a55f1c1616006bed846372e6e3", region: "电白"},
{id: "f47f682c5f1c163f008839ef2a343440", region: "茂名"},
{id: "3d23c0a05f1c1681008a5b3a0b8bb446", region: "茂名"},
{id: "f47f682c5f1c168c00883c273a954b1e", region: "茂南"},
{id: "08e51e265f1c16910070c8117bbe476c", region: "茂名"},
{id: "15d399db5f1c16ef0086f16b2a83a752", region: "茂名"},
{id: "08e51e265f1c1b340070e7675a7c02df", region: "茂名"},
{id: "9bf625a55f1c1b40006c0e721d7a7690", region: "高州"},
{id: "9bf625a55f1c1b44006c0e916570035a", region: "高州"},
{id: "3d23c0a05f1c1b52008a840d3b62a411", region: "高州"},
{id: "2a0398605f1c1b5600726d490bf434c9", region: "化州"},
]
var result = new Map()
data.forEach(item=>{
    if(result.has(item.region)){
        let m = result.get(item.region)
        m++
        result.set(item.region,m)
}else{
result.set(item.region,0)
}
console.log(result)
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 13:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表