Array.reduce() ?

Peter Castine's icon

ECMAScript 5 supports a method for Arrays called reduce().

The [js] object seems not to like this. "js: myJavaScript.js: Javascript TypeError: a.reduce is not a function, line 164"

Yes, the variable a is an array, freshly generated by a call to arrayfromargs(arguments).

I suppose I'll have to use forEach or [gasp!] a plain-vanilla for loop to get the result I want. Or am I missing something? As in: is there any way to get the js object to grok Array.reduce()?

Most other ECMAScript 5 methods are supported by, so I was a bit surprised about this.

Peter Castine's icon

PS: If Max6 will take care of this issue, that would also be useful to know.

withakay's icon

Javascript 1.6 (the version Max uses) does not support reduce natively but I don't see why you couldn't use a javascript library to add the functionality.
My favourite would be Underscore.js - http://documentcloud.github.com/underscore/

I can't guarantee this will work but I am 99% certain underscore has no reliance on the DOM so it should!

Let us know how you get on :)

Peter Castine's icon

OK, I was focusing on the ECMAScript version; ECMAScript 5 introduces all the nifty Array methods. Need to pay more attention to what the different JScript levels support.

Since there is currently only one place in my code where Array.reduce() would have been really nice to have, I've simply written a plain-vanilla for loop instead. Nothing against added libraries, but time and place…

Emmanuel Jourdan's icon

Array.reduce() works great in Max 6…

Peter Castine's icon

Nice to know. The other question, of course, would be « quand arrive-t-il ? » but I last time I looked there's no ETA.

Emmanuel Jourdan's icon

according to the announcement it's arriving this fall, that should narrow it down ;-)

Peter Castine's icon

Oh, there it is. Right at the top. Last place I look for that sort of thing. C'est ma faute.

Later down I see we're up to JS 1.8.5. Yummy. (Yes, I know, I used to be disdainful about the language. I still think automatic variable declaration is an absolutely execrable design decision, but there is a lot about the language to like.)