Bug Tracker

Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#4327 closed bug (invalid)

jQuery.map doesn't respect arrays

Reported by: borgar Owned by:
Priority: major Milestone: 1.4
Component: core Version: 1.3.2
Keywords: map Cc: borgar, flesler
Blocked by: Blocking:


The jQuery map method returns a merged array of values rather than an array of the values returned by the callback. This goes against the convention and implementation of almost every other library and programming language in the world.

In addition, the documentation for this function say: "The [callback] function can return any value." No mention that some return values are handled differently than others. This is at best very misleading.

jQuery test code:

  jQuery.map(['a:a','b:b', 'c:c'], function (a) {
    return a.split(':'); 

  // returns: ["a", "a", "b", "b", "c", "c"]

For comparison, same test in different languages:


  ['a:a','b:b', 'c:c'].map(function(x){
    return x.split(':'); 

  // returns: [["a", "a"], ["b", "b"], ["c", "c"]]


  map(lambda x: x.split(':'), ['a:a','b:b','c:c'])
  [['a', 'a'], ['b', 'b'], ['c', 'c']]

  //returns: [['a', 'a'], ['b', 'b'], ['c', 'c']]


  function test ($x) { return explode(':', $x); }
  print_r( array_map( 'test', array('a:a','b:b', 'c:c') ) );

  // returns : Array( Array('a','a'), Array('b','b'), Array('c','c') );

Change History (4)

comment:1 Changed 14 years ago by flesler

Cc: borgar flesler added
Resolution: invalid
Status: newclosed

That's on purpose. $.map has been doing that since always.

You can either write your own map, or wrap the returned array into another array.

return [a.split(':')];

comment:2 Changed 14 years ago by borgar

Resolution: invalid
Status: closedreopened

Is there a rationale for doing this? "Because it has always been this way" is not a reason for not fixing it. This logic would not apply to most other issues, or nothing would ever get fixed. If it is on purpose, then what problem is it there to solve?

At the very least, if the keeping this behaviour is desired, then I feel that rather than simply dismissing the ticket, it should be moved to docs so that this obscurity gets documented.

comment:3 Changed 14 years ago by dmethvin

Resolution: invalid
Status: reopenedclosed

The documentation states this:

The function can then return the translated value, 'null' (to remove the item), or an array of values - which will be flattened into the full array.


Looking at the page history, it appears to be documented this way for more than two years. Changing the long-documented behavior of an interface would probably break existing code.

comment:4 Changed 14 years ago by borgar

Whoops! My bad. I absolutely managed to miss this in the docs.

I guess I am too comfortable with them by now. I was too focused on the documentation for the callback parameter itself, which is what I was quoting the the ticket.

Note: See TracTickets for help on using tickets.