Bug Tracker

Opened 10 years ago

Closed 10 years ago

Last modified 8 years ago

#6183 closed bug (wontfix)

$.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']

Change History (5)

comment:1 Changed 10 years ago by john

Resolution: wontfix
Status: newclosed

Yep, that's intended - it's even mentioned in the docs: http://api.jquery.com/jQuery.map/

comment:2 Changed 9 years ago by anonymous

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.

comment:3 Changed 8 years ago by anonymous

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.

comment:4 Changed 8 years ago by dmethvin

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/

comment:5 Changed 8 years ago by Rick Waldron

Component: misccore
Priority: undecidedlow
Note: See TracTickets for help on using tickets.