Side navigation
#6879 closed enhancement (wontfix)
Opened August 05, 2010 10:54PM UTC
Closed October 27, 2010 06:03PM UTC
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: | |
Blocked by: | Blocking: |
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();
};
Attachments (0)
Change History (2)
Changed August 06, 2010 12:49AM UTC by comment:1
Changed October 27, 2010 06:03PM UTC by comment:2
priority: | → undecided |
---|---|
resolution: | → wontfix |
status: | new → closed |
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.
Note that this enhancement would mean the quick path using a native Array.indexOf couldn't be taken.