I am trying to filter my array:
case ON_SET_RECENT_SEARCH: {
const searchToSave = action.payload;
const filteredSearches = state.recentSearches.filter(
// function to check for duplicates - pass in current item and searchToSave to check against
!checkForDuplicate(item, searchToSave),
);
return {
...
};
}
checkForDuplicate function, console.log
never gets called. How can I filter my array using my helper function?
const checkForDuplicate = (item, searchToSave) => {
console.log('checkForDuplicate');
const isDuplicate =
(item.id === searchToSave.id &&
item.fuelType === searchToSave.fuelType &&
item.searchDistance === searchToSave.searchDistance) ||
(!!item.isCurrentLocation && !!searchToSave.isCurrentLocation);
return !isDuplicate;
};
2
Answers
The parameter to
.filter()
should be an function like below.The
filter()
method of Array instances receives a callback as the parameter and you were passing!checkForDuplicate(item, searchToSave)
which is a value (thecheckForDuplicate()
is executed and returns a value before thefilter()
is executed). Just need to update it asRead more from the Array.prototype.filter() docs here