Marc, if you want a map function that works the way you expect, use the
standard JavaScript one.

Here's an implementation from the Gecko reference that provides a map
function for JavaScript engines that don't have it built in:

if( ! Array.prototype.map ) {
    Array.prototype.map = function( fun /*, thisp*/ ) {
        var len = this.length;
        if( typeof fun != 'function' )
            throw new TypeError();
        
        var res = new Array( len );
        var thisp = arguments[1];
        for( var i = 0;  i < len;  ++i ) {
            if( i in this )
                res[i] = fun.call( thisp, this[i], i, this );
        }
        
        return res;
    };
}

So now you can do:

[ 1, 2, 3 ].map( function(n) {
    return [n, 'xxx'];
});

-Mike 

> From: Ariel Flesler
> 
> $.map is used a lot internally for everything. By the 
> selectors module, the manipulation/traversing methods, etc.
> This was the desired functionality in order to simplify its 
> internal duties.

> On 11 jun, 07:19, Marc Galera <[EMAIL PROTECTED]> wrote:
> > Hi.
> >
> > I've recently been surprised with $.map. I've found that it 
> flattens 
> > the result so I can't return an array in the callback. For 
> example, on 
> > this code:
> >
> > $.map([1, 2, 3], function(n) {
> >    return [n, 'xxx'];
> >
> > });
> >
> > I get:
> >
> > [1, "xxx", 2, "xxx", 3, "xxx"]
> >
> > Instead of what I expected:
> >
> > [[1, "xxx"], [2, "xxx"], [3, "xxx"]]
> >
> > Looking on the source code is obvious that this is made on purpose, 
> > and there are workarounds, but I was curious on why this is 
> happening.
> >
> > Marc.
> 

Reply via email to