They don't. Filter can reduce the number of elements in an array: only elements which return true (or a "truthy" value) will remain in the array after it's filtered. For example,

[1,2,3,4,5,6,7,8,9,10].filter(function(item) { return item < 6; }) == [1,2,3,4,5]

Map, by contrast, applies a function to each element in the array. For example,

[1,2,3,4,5,6,7,8,9,10].map(function(item) { return item * 2; }) == [2,4,6,8,10,12,14,16,18,20]

Aaron's assertion that you should use "filter" was a simple mistake, hence the "doh". :)

On 3/01/2010 8:03 PM, Rolf -nl wrote:
Why is there array.filter and array.map if they do the same?


On Jan 3, 7:48 am, Ryan Florence<[email protected]>  wrote:
It's odd, I use .map with ruby constantly, and have never used it in mootools ...

On Jan 2, 2010, at 10:36 PM, Aaron Newton wrote:



Doh!!!
-Aaron
Sorry for any typos. Big fingers , tiny buttons.
On Jan 2, 2010, at 7:03 PM, Perrin Perrin
<[email protected]>  wrote:
see array.maphttp://www.mootools.net/docs/core/Native/
Array#Array:map
Same function that Aron put up but with map instead of filter.
On Sat, Jan 2, 2010 at 8:54 PM, Aaron Newton<[email protected]> wrote:
see array.filter
myLinks.filter(function(link){ return link.get('href'); });
On Sat, Jan 2, 2010 at 6:50 PM, mmjaeger<[email protected]>  wrote:
stupid me - sorry - I need to get the hrefs into an array and then
encode it - thanks for replying
On Jan 2, 6:44 pm, Aaron Newton<[email protected]>  wrote:
DOM elements are not JSON encodable (what would you expect it to
do with
them?).
On Sat, Jan 2, 2010 at 6:31 PM, mmjaeger<[email protected]>
wrote:
Hello
I've the following code:
var arr = document.id(document.body).getElements('a[href$=mp3]');
console.log(arr.length); // RETURNS 6
console.log('JSON: ' + JSON.encode(arr)); // RETURNS JUST []
what am I missing?
Thank you

Reply via email to