How do you reduce an array of like-objects into a single object in JS? The Array.prototype...

How do you reduce an array of like-objects into a single object in JS? The Array.prototype.reduce method is confusing to me.

> inb3 meem lang

such as:

var data = [
{count: 1},
{count: 4}
];


with the expected output of:

{count: 5 }

also note that not all elements in the array are valid (that is, some are null, so the reduce(function (prev, next) ...) signature doesn't work.

>such as:
>var data = [
> {count: 1},
> {count: 4}
>];
>with the expected output of:
>{count: 5 }

Like:
var reduced = {count: 0};
for (var d in data){
reduced.count += data[d].count;
}

?
I could be wrong, I just learned JS last week

not quite. I am more interested in whether or not such a summation of object properties is able to accumulate onto a fresh object without knowledge of the structure of the original/subsequent objects.

Isn't this all it is?

var objectz = ["Cat", "Dog", "Cat", "Dog", "Frog", "Bird", "Frog"];
var reduced = [];

for(var i in objectz){
if(reduced.includes(objectz[i]) == false) reduced.push(objectz[i])
}

var data = [
{count: 1},
{count: 4}
];
var reduced = {};
for (var i = 0; i < data.length; i++) {
var obj = data[i];
for (var attr in obj) {
if (reduced.hasOwnProperty(attr)) reduced[attr] += obj[attr];
else reduced[attr] = obj[attr];
}
}

?

spot on!
thank you very much!

out of curiosity does anyone know the functional approach to this situation?

((((((((((((((eat)))))))))(((((((((shit)))))))))))))))

Does nobody on Sup Forums understand OP's question?

var data = [
{count: 1},
{count: 4},
];
function reducer(accumulator, value) {
return {count: accumulator.count + value.count};
}

Then call Array.prototype.reduce:
var result = data.reduce(reducer, {count: 0});

And then result will be {count: 5}.

>I am more interested in whether or not such a summation of object properties is able to accumulate onto a fresh object without knowledge of the structure of the original/subsequent objects.
Also
>spot on!
>thank you very much!

const pullStream = require('pull-stream');

pullStream(
pullStream.values(['eat', 'healthy']),
pullStream.sink(function (val) {
return console.info(val);
});
)

var data = [{count: 1}, {count: 4}]
function myReduceFunction (accumulator, val) {
accumulator.count += val.count // put everything into accumulator. it starts as the initial value.
return accumulator
// or // return {count: accumulator.count + val.count} // this creates a new object.
}
var initialValue = {count: 0} // should look just like 'data' objects.
var sum = data.reduce(myReduceFunction, initialValue); // reduce is an Array's built in method.
console.log(sum)

that only works when acc.count is valid, say we have an array:

[
{count: 1},
null,
{count: 2}
]


Array.prototype.reduce shits the bed too often

You shouldn't be mapping or reducing such fucked up mixed type arrays. Frankly you shouldn't generally have such mixed type arrays at all.

function myReduceFunction (accumulator, val) {
if (valueIsFine(val)) {
accumulator.count += val.count // put everything into accumulator. it starts as the initial value.
}
return accumulator
}

Learn2read
>I am more interested in whether or not such a summation of object properties is able to accumulate onto a fresh object without knowledge of the structure of the original/subsequent objects.

Goddamn right. is best I've seen so far

unsanitized csv entries :_{

do this to check if csv entry is usable

var data = [
{count: 1},
{shit: 23},
null,
{farts: 12},
{count: 4, farts: 4}
];
var reduced = {};
for (var i = 0; i < data.length; i++) {
var obj = data[i];
for (var attr in obj) {
if (reduced.hasOwnProperty(attr)) reduced[attr] += obj[attr];
else reduced[attr] = obj[attr];
}
}
console.log(reduced);

>Object {count: 5, shit: 23, farts: 16}

>how to combine objects using reduce
var data = [
{count: 1},
{shit: 23},
null,
{farts: 12},
{count: 4, farts: 4}
];
var result = data.reduce(function (accumulator, val) {
for (var attr in val) {
if (accumulator.hasOwnProperty(attr)) accumulator[attr] += val[attr];
else accumulator[attr] = obj[attr];
}
return accumulator
}, {})
console.log(result);

>Object {count: 8, shit: undefined, farts: 8}
You fail it