cqwcns 发表于 2020-7-29 08:07

JS数组对象统计的方法



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

[
{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进行统计,输出这样的结果:
{茂名: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) {
          data++;
      } else {
          data = 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) {
    ob=0
}
ob++;
}
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))

池中金麟 发表于 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

谢谢各位热心回复{:301_1003:}

田多 发表于 2020-7-29 08:58

js初学,学习了上面几个师兄的写法

clyzhi 发表于 2020-7-29 09:03

给出两种输出结果,第二种更符合题意
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 = (tempObj||0) + 1
})
console.log(tempObj)
let printSting = '{',i=0
for (let key in tempObj) {
    printSting += `${key}:${tempObj}${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)
页: [1] 2
查看完整版本: JS数组对象统计的方法