How to use reducer on each element of array of objects to calculate total of some value in JavaScript

1 Answer

0 votes
var workers = [
    { salary: 13451, name: 'Axel'},
    { salary: 87134, name: 'Blaze'},
    { salary: 79372, name: 'Dexter'},
    { salary: 39810, name: 'Isla'}
];
 
totalSalary = workers.reduce((accumulator, currentWorker) => {
	return accumulator + currentWorker.salary;
}, 0);

console.log(totalSalary);

 
 
 
/*
run:
     
219767

  
*/

 



answered Jun 16, 2020 by avibootz
...