Bug Tracker

Modify

Ticket #9944 (closed enhancement: plugin)

Opened 23 months ago

Last modified 16 months ago

Pass new array as third argument to jQuery.grep and jQuery.map callbacks

Reported by: anonymous Owned by:
Priority: low Milestone: None
Component: misc Version: 1.6.2
Keywords: needsreview Cc:
Blocking: Blocked by:

Description

Providing the new Array being constructed via jQuery.grep() to the callback provided would allow inquiry into the new Array that could be useful when determining the return value.

 https://github.com/jquery/jquery/blob/1.6.2/src/core.js#L722

Change:

retVal = !!callback( elems[ i ], i );

to:

       // added new Array as arg-----v
retVal = !!callback( elems[ i ], i, ret );

Possible usage example:

var arr = 
$.grep(
    $('div').map(function() { return $(this).attr('rel'); } ).get(),
    function( v, i, arr ) { return $.inArray( v, arr ) === -1; } // exclude dupes
);

jsfiddle:  http://jsfiddle.net/b7hDr/


I would also be inclined to suggest this for jQuery.map():

var arr = 
$.map($('div'), function( v, i, arr ) { 
    var rel = $(v).attr('rel'); 
    return $.inArray( rel, arr ) === -1 ? rel : void 0;
});

jsfiddle:  http://jsfiddle.net/b7hDr/1/

...although I don't know if it would interfere with the internal arg parameter.

 https://github.com/jquery/jquery/blob/1.6.2/src/core.js#L742

     // make room for "ret"?------v
value = callback( elems[ i ], i, ret, arg );

 https://github.com/jquery/jquery/blob/1.6.2/src/core.js#L752

     // make room for "ret"?----------v
value = callback( elems[ key ], key, ret, arg );

Change History

comment:1 Changed 22 months ago by addyosmani

  • Keywords needsreview added
  • Priority changed from undecided to low
  • Status changed from new to open
  • Component changed from unfiled to misc

I don't have any particular opinions on this enhancement, but I'm moving this to either the vote queue or for further review.

comment:2 Changed 16 months ago by dmethvin

  • Status changed from open to closed
  • Resolution set to plugin

Sorry but we don't need to complicate this highly-used code for edge cases. It would be better to do this as a plugin with the additional semantics.

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.