firefox - Why does reduceRight return NaN in Javascript? -
I am using Firefox 3.5.7 and inside firebug I am trying to test the array.reduceRight function I work, it works simple arrays But when I try to do something like I get NaN why?
& gt; & Gt; & Gt; Var Description = [{Score: 1}, {Score: 2}, {Score: 3}]; & Gt; & Gt; & Gt; Description [Object score = 1, Object score = 2, Object score = 3]> gt; & Gt; & Gt; description. Reduce (function (x, y) {return x.score + y.score;}, 0) nayn
I also tried to map and at least see me the .score component Of each element:
& gt; & Gt; & Gt; description. Map (function (x) {console.log (x.score);}) 1 2 3 [undefined, undefined, undefined]
I read the documentation but apparently I could call my Description Why is it not able to work for all scores values in the array?
The first argument given to the function is the accumulated value. The first call on the function will look like f (0, {Score: 1}). So when you're doing x.score, you're actually doing 0.so which does not definitely work. In other words, you want
x + y.score
.
Comments
Post a Comment