简体   繁体   English

计算javascript中每个值的出现次数

[英]Count no of occurrence of each value in javascript

I ended up with tedious loop to convert 我以繁琐的循环结束转换

[{Name: "Daniel Montes", color: "red"},
 {Name: "Daniel Montes", color: "red"},
 {Name: "Daniel Montes", color: "red"},
 {Name: "Michelle Aguirre", color: "red"},
 {Name: "Daniel Montes", color: "green"}
]

to

[
{Name: "Daniel Montes", green:1,red:3},
{Name: "Michelle Aguirre", green:0,red:1},
]

red and green are only 2 colors so that can be hardcoded as well. 红色和绿色只有2种颜色,因此也可以进行硬编码。

What is the best way to achieve it? 最好的方法是什么? Do we have anything on lodash or something equivalent? 我们在lodash上有东西吗?

 var input = [{Name: "Daniel Montes", color: "red"}, {Name: "Daniel Montes", color: "red"}, {Name: "Daniel Montes", color: "red"}, {Name: "Michelle Aguirre", color: "red"}, {Name: "Daniel Montes", color: "green"} ]; function solve(list){ var map = new Map(); var entry = null; for(var item of list){ if(!map.has(item.Name)) map.set(item.Name, {Name: item.Name}); entry = map.get(item.Name); if(entry.hasOwnProperty(item.color)) entry[item.color] = entry[item.color] + 1; else entry[item.color] = 1; } return Array.from(map.values()); } console.log(solve(input)) 

Here's a quick solution that I wrote up for your problem. 这是我为您撰写的快速解决方案。

Another approach is to use reduce . 另一种方法是使用reduce If a template object is used based on fixed properties Name , red , green , the code can be a little shorter. 如果基于固定属性Nameredgreen使用模板对象,则代码可能会短一些。

Both the following solutions use an index object to keep track of where named objects are in the result array, similar to pattpass' use of Map. 以下两种解决方案都使用索引对象来跟踪命名对象在结果数组中的位置,类似于pattpass对Map的使用。 The first solution only includes a color in the solution if the particular name has that color. 如果特定名称具有该颜色,则第一个解决方案仅在解决方案中包括一种颜色。 The second solution uses a template object with fixed properties so if an object doesn't have the color, it's in the result with a value of 0. 第二种解决方案使用具有固定属性的模板对象,因此,如果对象没有颜色,则结果为0。

The same result can be achieved by dynamically keeping track of colors, but there'd need to be logic to add the zero colors to objects that don't have them (maybe a loop at the end). 通过动态跟踪颜色可以实现相同的结果,但是需要逻辑将零颜色添加到不具有零颜色的对象中(可能是循环)。

 var input = [{Name: "Daniel Montes", color: "red"}, {Name: "Daniel Montes", color: "red"}, {Name: "Daniel Montes", color: "red"}, {Name: "Michelle Aguirre", color: "red"}, {Name: "Daniel Montes", color: "green"} ]; // Only add color if object has that color function process0(data) { var index = Object.create(null); return input.reduce(function (acc, obj) { var name = obj.Name, color = obj.color; // If haven't seen name before, add to index and // default object to accumulator if (!index[name]) { index[name] = acc.length; acc.push({Name:name}); } // If this name doesn't have the color as a property // ie red or green, add it if (!acc[index[name]].hasOwnProperty(color)) { acc[index[name]][color] = 0; } // Increment the color value ++acc[index[name]][color]; return acc; }, []); } console.log(process0(input)); // Use template object so if object doesn't have color, value is 0 // Same algorithm as above. function process1(data) { var index = Object.create(null); var temp = {red: 0, green: 0}; return input.reduce(function (acc, obj) { var name = obj.Name; if (!index[name]) { index[name] = acc.length; acc.push(Object.assign({}, {Name:name}, temp)); } ++acc[index[name]][obj.color]; return acc; }, []); } console.log(process1(input)); 

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

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