Skip to main content

Bug Tracker

Side navigation

#4327 closed bug (invalid)

Opened March 11, 2009 11:35AM UTC

Closed March 12, 2009 11:26PM UTC

Last modified March 13, 2009 10:32AM UTC 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:['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') );
Attachments (0)
Change History (4)

Changed March 11, 2009 10:08PM UTC by flesler comment:1

cc: → borgar, flesler
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(':')];

Changed March 12, 2009 12:06PM UTC by borgar comment:2

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.

Changed March 12, 2009 11:26PM UTC by dmethvin comment:3

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.

Changed March 13, 2009 10:32AM UTC by borgar comment:4

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.