Bug Tracker

Modify

Ticket #6183 (closed bug: wontfix)

Opened 3 years ago

Last modified 18 months ago

$.map flattens arrays

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

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

comment:1 Changed 3 years ago by john

  • Status changed from new to closed
  • Resolution set to wontfix

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

comment:2 Changed 2 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 18 months 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 18 months ago by dmethvin

  • Priority set to 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 18 months ago by rwaldron

  • Priority changed from undecided to low
  • Component changed from misc to core

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.