Bug Tracker

Modify

Ticket #6879 (closed enhancement: wontfix)

Opened 3 years ago

Last modified 3 years ago

Add custom comparator to inArray

Reported by: kbwood Owned by:
Priority: undecided Milestone: 1.4.3
Component: misc Version: 1.4.2
Keywords: inArray Cc:
Blocking: Blocked by:

Description

As an enhancement, add a third parameter to inArray that is a callback function for a custom equality comparison. This would allow for the matching of custom objects within an array without having to have a reference to the original object. For example, you could find a Date within an array like this:

var d1 = new Date(2010, 1-1, 1);

var d2 = new Date(2010, 2-1, 1);

var d3 = new Date(2010, 1-1, 1);

var dates = [d1, d2];

var index = $.inArray(d3, dates, function(a, b) {

return a.getTime() == b.getTime();

});

Maybe there could even be common comparators built-in, such as the date one above.

$.comparator.date = function(a, b) {

return a.getTime() == b.getTime();

};

Change History

comment:1 Changed 3 years ago by dmethvin

Note that this enhancement would mean the quick path using a native Array.indexOf couldn't be taken.

comment:2 Changed 3 years ago by SlexAxton

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

inArray is an implementation of the ES5 spec, if I recall correctly. I think keeping it that way is important. It would be easy to add a plugin/wrapper in your app to do this though.

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.