Skip to main content

Bug Tracker

Side navigation

#6183 closed bug (wontfix)

Opened February 26, 2010 04:15PM UTC

Closed February 26, 2010 05:42PM UTC

Last modified November 19, 2011 09:58PM UTC

$.map flattens arrays

Reported by: nlogax Owned by:
Priority: low Milestone: 1.4.3
Component: core Version: 1.4.2
Keywords: Cc:
Blocked by: Blocking:
Description

Mapping function(x) { return x; } over an array should result in an identical array.

Using the Array map method, this works as expected:

[[1,2,3], ['a','b','c']].map(function(x) { return x; }) -> [[1,2,3], ['a','b','c']]

Using jQuery.map, this happens:

$.map([[1,2,3], ['a','b','c']], function(x) { return x; }) -> [1, 2, 3, 'a', 'b', 'c']

Attachments (0)
Change History (5)

Changed February 26, 2010 05:42PM UTC by john comment:1

resolution: → wontfix
status: newclosed

Yep, that's intended - it's even mentioned in the docs:

http://api.jquery.com/jQuery.map/

Changed March 03, 2011 04:03PM UTC by anonymous comment:2

This is really broken for a map function. It shouldn't flatten or remove nulls, whether or not it says it's broken in the documentation.

Changed November 18, 2011 11:42PM UTC by anonymous comment:3

It would be interesting to understand why the implementation needs to flatten arrays and why we can't control the behavior with an optional param. If you attempt to use the map function on an array of arrays, this behavior is inappropriate. If you review the comments on the map page, a number of people find this non-obvious and unexpected flattening to be a problem. It's easy enough to wrap your value in a new array to work around the problem, but it feels like a hack.

Changed November 19, 2011 12:13AM UTC by dmethvin comment:4

priority: → undecided
It would be interesting to understand why the implementation needs to flatten arrays and why we can't control the behavior with an optional param.

First and foremost, $.map is a utility function intended for our own use with DOM elements; we need it to flatten arrays and remove nulls. We're not interested in functionality that would add size and reduce performance if we don't need it.

An Array-wrapped array seems less funky than passing a boolean argument that changes the behavior, and it's fully backward-compatible.

http://jsfiddle.net/2x2tg/

Changed November 19, 2011 09:58PM UTC by rwaldron comment:5

component: misccore
priority: undecidedlow