I know I can use reduce to sum the numbers of an array, like so:
let numArray = [1, 2, 3, 4].reduce((a, b) => a + b, 0);
console.log(numArray);
However, if I have an array of objects, of which one field's value is a number, how would I go about getting the sum of just that field for the objects in the array?
Say for instance for this kind of data structure:
[
{
name: "John Smith",
balance: 42
},
{
name: "Jane Doe",
balance: 24
}
]
How would I use reduce to get the sum of just the "balance" field for all the objects in the array?