Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

reduce method in javascript

const months = ['april','may','june','may','may','june'];

const countDuplicates = months.reduce((obj,month)=>{
    if(obj[month] == undefined){
        obj[month] = 1;
        return obj;
    }else{
        obj[month]++;
        return obj;
    }
},{});
console.log(countDuplicates);//output:{april: 1, may: 3, june: 2}
Source by www.javascripttutorial.net #
 
PREVIOUS NEXT
Tagged: #reduce #method #javascript
ADD COMMENT
Topic
Name
1+7 =