Javscript filter vs map problem

Posted by graham.reeds on Stack Overflow See other posts from Stack Overflow or by graham.reeds
Published on 2010-03-14T16:22:37Z Indexed on 2010/03/14 16:25 UTC
Read the original article Hit count: 274

Filed under:
|
|

As a continuation of my min/max across an array of objects I was wondering about the performance comparisons of filter vs map.

So I put together a test on the values in my code as was going to look at the results in FireBug.

This is the code:

var _vec = this.vec;
min_x = Math.min.apply(Math, _vec.filter(function(el){ return el["x"]; }));
min_y = Math.min.apply(Math, _vec.map(function(el){ return el["x"]; }));

The mapped version returns the correct result. However the filtered version returns NaN. Breaking it out, stepping through and finally inspecting the results, it would appear that the inner function returns the x property of _vec but the actual array returned from filter is the unfiltered _vec.

I believe my usage of filter is correct - can anyone else see my problem?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about filter