Array.reduce not a function?

1,636 views
Skip to first unread message

Josh K

unread,
Sep 21, 2010, 2:18:08 PM9/21/10
to mongodb-user
Is it, or should it be possible, to write

return v.reduce(function(p,c){ return p+c; }, 0);

in a reduce function as part of a M / R call? I know that V8 (and
Node.js) accept a reduce call, but does SpiderMonkey?

Thanks,

-Josh

Kristina Chodorow

unread,
Sep 21, 2010, 2:31:50 PM9/21/10
to mongod...@googlegroups.com
Yes, SpiderMonkey has this function, too.



--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Joshua Kehn

unread,
Sep 21, 2010, 2:36:56 PM9/21/10
to mongod...@googlegroups.com
From the MongoShell I can't seem to access it.

> v = [1, 2, 3]
[ 1, 2, 3 ]
> var res = v.reduce(function(p,c){ return p+c; }, 0)
Tue Sep 21 14:35:54 TypeError: v.reduce is not a function (shell):0

Something I'm doing wrong? The above code runs find under V8.

Thanks,

-Josh
____________________________________
Joshua Kehn | Josh...@gmail.com

Kristina Chodorow

unread,
Sep 21, 2010, 2:40:35 PM9/21/10
to mongod...@googlegroups.com
What version are you running?  How did you download/install it?

(From my shell:

> [1,2,3].reduce(function(p,c) { return p+c; }, 0)                             
6
> v = [1,2,3]
[ 1, 2, 3 ]

> v.reduce(function(p,c) { return p+c; }, 0)
6

)

Joshua Kehn

unread,
Sep 21, 2010, 2:45:29 PM9/21/10
to mongod...@googlegroups.com
Running 1.6.2, installed from the OS X binaries. 

Thanks,

-Josh
____________________________________
Joshua Kehn | Josh...@gmail.com

Kristina Chodorow

unread,
Sep 21, 2010, 3:41:10 PM9/21/10
to mongod...@googlegroups.com
Unfortunately, it looks like the reduce function was added in SpiderMonkey 1.8 and the OS X machines are running SpiderMonkey 1.7.  I think this will be resolved soon (we're going to be using a single version across all the builds), but it wouldn't hurt to file a bug if you need it.

Joshua Kehn

unread,
Sep 21, 2010, 3:45:59 PM9/21/10
to mongod...@googlegroups.com
Not immediately, this is actually in relation to a question I asked on SO, fastest way to sum an array. That was given. I will just use a loop for now. 

Thanks,

-Josh
____________________________________
Joshua Kehn | Josh...@gmail.com

Reply all
Reply to author
Forward
0 new messages