Can anyone explain the == -1 at the end of this callback function?
function destroyer(arr) {
var args = Array.from(arguments);
args.shift();
return arr.filter(function(element) {
return args.indexOf(element) == -1;
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
args.indexOf(element)
returns -1
if element
is not found anywhere in the args
array.
args.indexOf(element) == -1
will evaluate to true
if element
is not in the args
array. This expression is equivalent to !args.includes(element)
.
Read more about indexOf()
and includes()
.
1 Like
indexOf returns -1 if element (from arr) is not present in args. So the whole equality check is true for elements that are not in args and false otherwise. Filter only retain elements for which the callback returns true.
1 Like
I assumed it was related to .filter(); It all makes sense now. Thank you.