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 }
>>59995835
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.
>>59995835
>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
>>59995864
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.
>>59995809
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])
}
>>59995809
>>59995864var 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];
}
}
?
>>59996257
spot on!
thank you very much!
out of curiosity does anyone know the functional approach to this situation?
>>59996328
((((((((((((((eat)))))))))(((((((((shit)))))))))))))))
>>59995835
>>59995864
>>59996135
>>59996257
Does nobody on /g/ 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}.
>>59996415
>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!
>>59996341const pullStream = require('pull-stream');
pullStream(
pullStream.values(['eat', 'healthy']),
pullStream.sink(function (val) {
return console.info(val);
});
)
>>59996328var 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)
>>59996415
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
>>59996523
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.
>>59996523function myReduceFunction (accumulator, val) {
if (valueIsFine(val)) {
accumulator.count += val.count // put everything into accumulator. it starts as the initial value.
}
return accumulator
}
>>59996520
>>59996506
>>59996415
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.
>>59996523
Goddamn right. >>59996257 is best I've seen so far
>>59996543
unsanitized csv entries :_{
>>59996560
do this to check if csv entry is usable >>59996552
>>59996543
>>59996257var 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}
>>59996602
>how to combine objects using reducevar 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);
>>59996643
>Object {count: 8, shit: undefined, farts: 8}
You fail it