I have the following array filter with a callback function:
array.filter(createPredicateFn(expression, comparator));
My call back function is declared as follow:
function createPredicateFn(expression, comparator) {
How can I get the index of the element inside my createPredicateFn?
Edit:
here is my predicate function:
function createPredicateFn(expression, comparator) {
var predicateFn;
if (comparator === true) {
comparator = angular.equals;
} else if (!angular.isFunction(comparator)) {
comparator = function (actual, expected) {
if (angular.isObject(actual) || angular.isObject(expected)) {
// Prevent an object to be considered equal to a string like `'[object'`
return false;
}
actual = angular.lowercase('' + actual);
expected = angular.lowercase('' + expected);
return actual.indexOf(expected) !== -1;
};
}
predicateFn = function (item) {
return deepCompare(item, expression, comparator);
};
}
Within your createPredicateFn()
you have to return another function. This inner function can have up to three parameters:
Hence the second parameter of your inner function is the index you look for.
A simplistic example could look like this:
function createPredicateFn(expression, comparator) {
return function( val, index, arr ) {
// do something and return a boolean here
return index % 2 == 0;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.