首页 > 解决方案 > 将对象的总数推送到数组

问题描述

我正在尝试计算特定项目在数组中出现的次数。然后将该数量推送到另一个数组。我让计数器工作了,尽管当我将这个数量推送到数组时,这个值的类型变成了不是数字......

这是我的代码:

res2 = 
0: (2) [29682885, "Jean-Paul"]
1: (2) [29682886, "DEMO Martin"]
2: (2) [29682887, "Johan"]
3: (2) [29682892, "Peter"]
4: (2) [29682900, "Antoine"]
5: (2) [29682902, "Sandra"]
6: (2) [29682906, "Kevin"]
7: (2) [29682910, "Wouter"]
8: (2) [29682911, "Tom"]
9: (2) [4, "Autotask"]

res3 =
0: (2) [29682885, "2019-05-16T08:25:32Z"]
1: (2) [29682885, "2019-07-01T13:11:00Z"]
2: (2) [29682885, "2019-07-03T10:21:07Z"]
3: (2) [29682885, "2019-09-03T14:00:45Z"]
4: (2) [29682885, "2019-09-11T09:59:07Z"]
5: (2) [29682885, "2019-09-17T14:13:39Z"]
6: (2) [29682885, "2019-10-09T16:48:41Z"]
7: (2) [29682885, "2019-10-30T13:48:12Z"]
8: (2) [29682885, "2019-10-30T14:13:01Z"]
9: (2) [29682885, "2019-10-30T14:34:13Z"]
10: (2) [29682885, "2019-11-07T13:41:27Z"]
11: (2) [29682885, "2019-11-22T12:41:08Z"]
...

res2.sort();
res3.sort();

res3.forEach(sale => {
    res2.forEach(person => {
        if (sale[0] === person[0]) {
            if (person[1] === undefined) {
                person[1] = 1;
                console.log(person[1]);
            } else {
                person[1].occurrences++;
                console.log(person[1]);
               
                prepArray.push(person[1]);
            }
            prepArray.push(person[1]);
        }
    }); 
});

prepArray.push(person[1]);以某种方式重新运行 NaN。但是控制台显示了我想推送到我的 prepArray 的确切数量......

在此处输入图像描述

标签: javascriptarraysobjectcount

解决方案


我使用了filter, flat,的混合map来实现这一点。添加了代码注释以解释正在使用的逻辑。

res2 = [[29682885, "Jean-Paul"],
[29682886, "DEMO Martin"],
[29682887, "Johan"],
[29682892, "Peter"],
[29682900, "Antoine"],
[29682902, "Sandra"],
[29682906, "Kevin"],
[29682910, "Wouter"],
[29682911, "Tom"],
[4, "Autotask"]]

res3 = [[29682885, "2019-05-16T08:25:32Z"],
[29682885, "2019-07-01T13:11:00Z"],
[29682902, "2019-07-03T10:21:07Z"],
[29682885, "2019-09-03T14:00:45Z"],
[29682885, "2019-09-11T09:59:07Z"],
[29682885, "2019-09-17T14:13:39Z"],
[29682902, "2019-10-09T16:48:41Z"],
[4, "2019-10-30T13:48:12Z"],
[4, "2019-10-30T14:13:01Z"],
[29682911, "2019-10-30T14:34:13Z"],
[29682911, "2019-11-07T13:41:27Z"],
[29682911, "2019-11-22T12:41:08Z"]];

// pick only the ids from res3
// this makes it easier for comparison
let res = res3.map(v => {
  return v.shift();
}).flat()

let final = [];

// loop over res2, and count occurrences using .filter().length
res2.map(sale => final.push([sale[1], res.filter(person => person === sale[0]).length]));

console.log(final);


// if you want the output as an object,
// you can do the following

let finalObj = {};

res2.map(sale => finalObj[sale[1]] = res.filter(person => person === sale[0]).length);

console.log({ finalObj })

res3稍微修改了输入集 ( ) 以显示计数的差异,上面的代码输出以下内容,

[
  [
    "Jean-Paul",
    5
  ],
  [
    "DEMO Martin",
    0
  ],
  [
    "Johan",
    0
  ],
  [
    "Peter",
    0
  ],
  [
    "Antoine",
    0
  ],
  [
    "Sandra",
    2
  ],
  [
    "Kevin",
    0
  ],
  [
    "Wouter",
    0
  ],
  [
    "Tom",
    3
  ],
  [
    "Autotask",
    2
  ]
]

编辑了我的答案以获得所需的对象格式输出,所以现在你会得到,

{
    "Jean-Paul": 5,
    "DEMO Martin": 0,
    "Johan": 0,
    "Peter": 0,
    "Antoine": 0,
    "Sandra": 2,
    "Kevin": 0,
    "Wouter": 0,
    "Tom": 3,
    "Autotask": 2
}

推荐阅读