#2616 closed enhancement (fixed)
A better jQuery.map
Reported by: | flesler | Owned by: | danheberden |
---|---|---|---|
Priority: | high | Milestone: | 1.6 |
Component: | core | Version: | 1.5 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Hi, today I realized jQuery.map doesn't support hashes(associative arrays). I'm surprised, this is a behavior I'd assume it'd be included.
I added the changes to make this possible. In addition, added a third, optional, argument that is the return object. So it's possible to map on a existing object.
I made it so flexible, that one can map from an array to a hash, and the other way around as well. This is mostly useful to collect the keys or values from a hash, or to map an array of elements to an object(like setArray). It will handle array-like objects, and will treat them as arrays
I optimized the array concatenation, so now it's only done once, in the end, and now, there's no need for array-wrapping the returned data.
I think the length of the code remains more or less the same, so this should be doable.
I tested all the cases in IE6, FF2, Safari 3, and Opera 9.
I hope this helps
Attachments (3)
Change History (26)
Changed 15 years ago by
comment:1 Changed 15 years ago by
Tried all these cases in IE6, FF2, Opera 9.25 and Safari 3 and they all worked well.
var keys = $.map( {a:1,b:2}, function( v, k ){ return k; }, [ ] ); alert(keys); var values = $.map( {a:1,b:2}, function( v, k ){ return v; }, [ ] ); alert(values); var mapped = $.map( document.getElementsByTagName('script'), function( v, k ){ return v; }, {length:0} ); alert(mapped); var flat = $.map( Array(8), function( v, k ){ return k % 2 ? k : [k,k,k];//try mixing array and regular returns }); alert(flat);
comment:4 Changed 15 years ago by
Your patch must be tested against the benchmark before applying it, as map is a core function of jQuery. Its raw speed has higher priority then being useful for other cases. If we can do both, even better.
comment:5 Changed 15 years ago by
Shouldn't you/we accept the change before adding the tests to the test suite ? Or it will fail for others that haven't applied the proposed diff.
comment:6 Changed 15 years ago by
I meant you/we add the test, not accept. I didn't mean to say it's on me to accept :)
comment:18 Changed 13 years ago by
Milestone: | 1.2.4 → 1.5 |
---|---|
Owner: | set to flesler |
Priority: | major → low |
Status: | new → pending |
Is this still something you guys think should make it into core? It's been a little while...
comment:19 Changed 13 years ago by
Status: | pending → closed |
---|
Automatically closed due to 14 days of inactivity.
comment:20 Changed 13 years ago by
I think the performance his is too much as per joern's comment: http://jsfiddle.net/jboesch26/mmhwZ/4/
comment:21 Changed 13 years ago by
I've made some changes to make sure this doesn't affect performance that much: https://github.com/jquery/jquery/pull/238
comment:23 Changed 13 years ago by
I've spent some time with the newest patch and perf comparing it to the existing, I'm indifferent about the perf. My issue is this...
// Provided test... var keys = jQuery.map( {a:1,b:2}, function( v, k ){ return k; }); equals( keys.join(""), "ab", "Map the keys from a hash to an array" ); // But this is totally bonkers: var keys = jQuery.map( {a:1,b:2, length: 2 }, function( v, k ){ return k; }); equals( keys.join(""), "ab", "Map the keys from a hash to an array, has a length property" ); // > "01" // expected: "ab"
comment:24 Changed 13 years ago by
Milestone: | 1.5 |
---|---|
Status: | closed → reopened |
comment:25 Changed 13 years ago by
Keywords: | needsreview added; array map removed |
---|---|
Milestone: | → 1.next |
Version: | 1.2.3 → 1.5 |
Reopening this for the current review
comment:26 Changed 13 years ago by
Updated pull request can be found here: https://github.com/jquery/jquery/pull/252
comment:27 Changed 13 years ago by
Status: | reopened → pending |
---|
Did you look at the weird test results I posted previously?
comment:28 Changed 13 years ago by
Resolution: | → invalid |
---|---|
Status: | pending → closed |
Because we get so many tickets, we often need to return them to the initial reporter for more information. If that person does not reply within 14 days, the ticket will automatically be closed, and that has happened in this case. If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Thanks!
comment:29 Changed 13 years ago by
Resolution: | invalid |
---|---|
Status: | closed → reopened |
comment:30 Changed 13 years ago by
Keywords: | needsreview removed |
---|---|
Milestone: | 1.next → 1.6 |
Priority: | low → high |
comment:31 Changed 12 years ago by
Owner: | changed from flesler to danheberden |
---|---|
Status: | reopened → assigned |
comment:33 Changed 12 years ago by
Why map to the object creates an array? I do not quite understand the history of this ticket, but the original patch was different behavior (map object to object) and I think it's more logical.
comment:34 Changed 12 years ago by
For an end use standpoint, you're right - but internally, worrying about something else being returned from map besides an array is out of scope for the patch. Even then, the patch isn't designed to be full featured by any means - just a simple way to parse out keys or values into an array.
That's not to say someone can't make a plugin to offer the object to object map - not sure how that'd look recursively though..
Proposal