Opened 12 years ago
Closed 12 years ago
#6879 closed enhancement (wontfix)
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();
};
Change History (2)
comment:1 Changed 12 years ago by
comment:2 Changed 12 years ago by
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.