简体   繁体   English

计算大量数据

[英]Counting a Large Amount of Data

So, I have two scripts running in Node.js.因此,我在 Node.js 中运行了两个脚本。

Script A outputs a whole number that is >= 40 and is <= 320.脚本 A 输出 >= 40 且 <= 320 的整数。

Script B runs script A millions of times and needs to count how many times each number appears.脚本 B 运行脚本 A 数百万次,需要计算每个数字出现的次数。

What is the best way to go about doing this? go 关于这样做的最佳方法是什么? There has to be a better solution than typing let number40 = 0; let number 41 = 0;必须有比输入let number40 = 0; let number 41 = 0; let number40 = 0; let number 41 = 0; all the way up to 320.一直到320。

How about having a map-enum-like object holding key as number and as per their occurrences, incrementing their value;如何拥有一个map-enum-like object 将key作为number并根据它们的出现来增加它们的值;

 let obj = {}; let numbers = [50, 55, 120, 300, 150, 50, 300, 50]; numbers.forEach(function(num) { obj[num]? ++obj[num]: (obj[num] = 1); }); console.log(obj);

Useing Map Map

 let myMap = new Map(); let numbers = [50, 55, 120, 300, 150, 50, 300, 50]; numbers.forEach(function(num) { let val = myMap.get(num) || 0; myMap.set(num, ++val); }); console.log(myMap);//check browsers console to check the value.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM