简体   繁体   中英

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.

What is the best way to achieve it? Do we have anything on lodash or something equivalent?

 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 . If a template object is used based on fixed properties Name , red , green , the code can be a little shorter.

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. 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.

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)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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