Skip to main content

Bug Tracker

Side navigation

#9944 closed enhancement (plugin)

Opened July 30, 2011 07:51PM UTC

Closed March 10, 2012 04:25AM UTC

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

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

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.


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


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

Possible usage example:

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


I would also be inclined to suggest this for

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


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

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

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

Attachments (0)
Change History (2)

Changed August 18, 2011 06:41AM UTC by addyosmani comment:1

component: unfiledmisc
keywords: → needsreview
priority: undecidedlow
status: newopen

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

Changed March 10, 2012 04:25AM UTC by dmethvin comment:2

resolution: → plugin
status: openclosed

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.