[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

How do you reduce an array of like-objects into a single object

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 23
Thread images: 3

File: 6Dl2Gyw.jpg (609KB, 1280x1600px) Image search: [Google]
6Dl2Gyw.jpg
609KB, 1280x1600px
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
>>
File: weeU1zz.jpg (1013KB, 3629x2722px) Image search: [Google]
weeU1zz.jpg
1013KB, 3629x2722px
>>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
>>59995864
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];
}
}

?
>>
File: 3afd0y56tw7y.png (438KB, 1431x1041px) Image search: [Google]
3afd0y56tw7y.png
438KB, 1431x1041px
>>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!
>>
>>59996341
const pullStream = require('pull-stream');

pullStream(
pullStream.values(['eat', 'healthy']),
pullStream.sink(function (val) {
return console.info(val);
});
)
>>
>>59996328
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)
>>
>>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.
>>
>>59996523
function 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
>>59996257
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}
>>
>>59996602
>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);
>>
>>59996643
>Object {count: 8, shit: undefined, farts: 8}
You fail it
Thread posts: 23
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.