Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34365
| From | Joao Rodrigues <groups_jr-1@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: get average height in an object |
| Date | 2017-11-16 02:46 -0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <ouj56o$rli$1@gioia.aioe.org> (permalink) |
| References | (1 earlier) <ou8vaq$1l44$1@gioia.aioe.org> <ou9q0i$1a7k$1@gioia.aioe.org> <ouhllb$oi1$1@gioia.aioe.org> <XnsA82EE37626AD1eejj99@194.109.6.166> <XnsA82EE996CA769eejj99@194.109.6.166> |
Evertjan wrote:
>
>> Joao Rodrigues <groups_jr-1@yahoo.com> wrote on 15 Nov 2017 in
>> comp.lang.javascript:
>>
>>> const data = {
>>> "Matt" : { "height" : 174, "weight": 69 },
>>> "Jason" : { "height" : 190, "weight": 103 }
>>> };
>>>
>>> let arr = Object.values(data);
>>> let avg = arr.reduce((a,c) => a.height + c.height) / arr.length;
>>> console.log(avg); // 182
>>
>> This will fail for:
>>
>> const data = {
>> "Matt" : { "height" : 174, "weight": 69 },
>> "Jason" : { "height" : 190, "weight": 103 },
>> "John" : { "height" : 80, "weight": 83 }
>> }; >
> So try this:
>
> let arr = Object.values(data);
> let avg = arr.reduce((r, v) => {
> return r + v.height;
> }, 0) / arr.length;
> console.log('average: ' + avg);
>
>
> and if you want to see what is going on in the reduce():
>
> let arr = Object.values(data);
> let avg = arr.reduce((r, v) => {
> console.log(r, r + v.height);
> return r + v.height;
> }, 0) / arr.length;
> console.log('average: ' + avg);
>
My mistake! Thanks for spotting the errors!
According to MDN [1], the reduce() method will execute the callback
function in different ways depending on the existence of the optional
initialValue argument.
As I didn't provide an initialValue of 0, I should have returned an
Object for the next iteration within reduce(). For instance:
var arr = Object.values(data);
var sum = (prev, cur) => ({height: prev.height + cur.height});
var avg = arr.reduce(sum).height / arr.length;
console.log(avg); // 148
And providing an initialValue of 0 as you did, reduce() will work
differently:
var arr = Object.values(data);
var sum = (accumulator, obj) => accumulator + obj.height;
var avg = arr.reduce(sum, 0) / arr.length;
console.log(avg); // 148
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce>
--
Joao Rodrigues
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
get average height in an object JRough <janis.rough@gmail.com> - 2017-11-11 14:57 -0800
Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-12 06:04 -0200
Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-12 06:49 -0200
Re: get average height in an object John G Harris <niam@jghnorth.org.uk.invalid> - 2017-11-12 13:39 +0000
Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-12 13:40 -0200
Re: get average height in an object JRough <janis.rough@gmail.com> - 2017-11-13 12:06 -0800
Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-15 13:14 -0200
Re: get average height in an object "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-15 22:21 +0100
Re: get average height in an object "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-15 22:57 +0100
Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-16 02:46 -0200
Re: get average height in an object "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-17 13:06 +0100
Re: get average height in an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-12 18:24 +0100
Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-13 00:26 -0200
Re: get average height in an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-13 03:42 +0100
Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-13 01:23 -0200
Re: get average height in an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-13 14:16 +0100
Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2017-11-13 14:20 -0300
Dictionary-like objects (was: get average height in an object) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-13 19:56 +0100
Re: Dictionary-like objects Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2017-11-13 20:26 -0200
Re: Dictionary-like objects Julio Di Egidio <julio@diegidio.name> - 2017-11-13 15:48 -0800
Re: Dictionary-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-15 02:55 +0100
Re: Dictionary-like objects Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-15 12:30 -0200
Re: Dictionary-like objects John G Harris <niam@jghnorth.org.uk.invalid> - 2017-11-15 15:59 +0000
Re: get average height in an object JRough <janis.rough@gmail.com> - 2017-11-13 12:08 -0800
Re: get average height in an object John G Harris <niam@jghnorth.org.uk.invalid> - 2017-11-13 10:17 +0000
Re: get average height in an object JRough <janis.rough@gmail.com> - 2017-11-13 12:07 -0800
Re: get average height in an object "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-12 11:00 +0100
Re: get average height in an object "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-12 11:15 +0100
Re: get average height in an object JRough <janis.rough@gmail.com> - 2017-11-13 12:28 -0800
csiph-web