Sum of array object property values in new array of objects in Javascript
Simple reduce
solution :
const data = [
{ subject: 'Maths', marks: '40', noOfStudents: '5' },
{ subject: 'Science', marks: '50', noOfStudents: '16' },
{ subject: 'History', marks: '35', noOfStudents: '23' },
{ subject: 'Science', marks: '65', noOfStudents: '2' },
{ subject: 'Maths', marks: '30', noOfStudents: '12' },
{ subject: 'History', marks: '55', noOfStudents: '20' }];
const result = data.reduce((cur, val) => {
let alreadyIn = cur.find(e => e['subject'] == val['subject']);
if (alreadyIn) {
alreadyIn['marks'] = (parseInt(alreadyIn['marks']) + parseInt(val['marks'])).toString();
alreadyIn['noOfStudents'] = (parseInt(alreadyIn['noOfStudents']) + parseInt(val['noOfStudents'])).toString();
} else {
cur.push(val);
}
return cur;
}, []);
console.log(result);
You can use forEach()
to iterate and generate the new array
var inputArray = [
{ subject: 'Maths', marks: '40', noOfStudents: '5' },
{ subject: 'Science', marks: '50', noOfStudents: '16' },
{ subject: 'History', marks: '35', noOfStudents: '23' },
{ subject: 'Science', marks: '65', noOfStudents: '2' },
{ subject: 'Maths', marks: '30', noOfStudents: '12' },
{ subject: 'History', marks: '55', noOfStudents: '20' }
],
res = [],
key = {};
inputArray.forEach(function(v) {
if (key.hasOwnProperty(v.subject)) { // check subject already added by using key object
res[key[v.subject]].marks += Number(v.marks); //incase already exist parse number and add
res[key[v.subject]].noOfStudents += Number(v.noOfStudents);
} else {
key[v.subject] = res.length; // create index entry in key object
res.push({ // push the value
'subject': v.subject,
'marks': Number(v.marks),
'noOfStudents': Number(v.noOfStudents)
})
// if you pushed the original object then the original array also will get updated while adding the mark, so never push the refernce
}
})
console.log(res);
Using reduce()
method
var inputArray = [
{ subject: 'Maths', marks: '40', noOfStudents: '5' },
{ subject: 'Science', marks: '50', noOfStudents: '16' },
{ subject: 'History', marks: '35', noOfStudents: '23' },
{ subject: 'Science', marks: '65', noOfStudents: '2' },
{ subject: 'Maths', marks: '30', noOfStudents: '12' },
{ subject: 'History', marks: '55', noOfStudents: '20' }
],
key = {};
res=inputArray.reduce(function(arr,v) {
if (key.hasOwnProperty(v.subject)) { // check subject already added by using key object
arr[key[v.subject]].marks += Number(v.marks); //incase already exist parse number and add
arr[key[v.subject]].noOfStudents += Number(v.noOfStudents);
} else {
key[v.subject] = arr.length; // create index entry in key object
arr.push({ // push the value
'subject': v.subject,
'marks': Number(v.marks),
'noOfStudents': Number(v.noOfStudents)
})
// if you pushed the original object then the original array also will get updated while adding the mark, so never push the refernce
}
return arr;
},[])
console.log(res);
FYI : You can avoid the key
object by using find()
method, but performance wise that may be little bit slower.