I want to remove all elements from the initial array that are of the same value as these arguments.
Ex:
destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
Here's my code:
function takeaway(value) {
return ??
}
function destroyer(arr) {
// Remove all the values\
var args = Array.from(arguments); // args = [[1,2,3,1,2,3],2,3]
var arr1 = args.shift(); // arr1 = [1, 2, 3, 1, 2, 3]
// args = [2,3]
var filtered = arr1.filter(takeaway);
return filtered;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
If I'm not mistaken I need to pass the elements that I want to take out (the args
array) into the filter function so it knows what to filter out... How would I acpish this?
I want to remove all elements from the initial array that are of the same value as these arguments.
Ex:
destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
Here's my code:
function takeaway(value) {
return ??
}
function destroyer(arr) {
// Remove all the values\
var args = Array.from(arguments); // args = [[1,2,3,1,2,3],2,3]
var arr1 = args.shift(); // arr1 = [1, 2, 3, 1, 2, 3]
// args = [2,3]
var filtered = arr1.filter(takeaway);
return filtered;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
If I'm not mistaken I need to pass the elements that I want to take out (the args
array) into the filter function so it knows what to filter out... How would I acpish this?
4 Answers
Reset to default 4Try the includes function of the array. If the arr1
element isn't included in the args array it returns false and only the elements that returns true are filtered out.
function destroyer(arr) {
// Remove all the values\
var args = Array.from(arguments); // args = [[1,2,3,1,2,3],2,3]
var arr1 = args.shift(); // arr1 = [1, 2, 3, 1, 2, 3]
// args = [2,3]
var filtered = arr1.filter(function(value){
return !args.includes(value);
});
return filtered;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3)
const destroyer = (arr, ...nopes) =>
arr.filter(value => !nopes.includes(value))
destroyer([1, 2, 3, 1, 2, 3], 2, 3)
Because it's harder to understand the type signature, I'd shy away from using variadic functions though in favor of explicit arrays like this. Flipping the argument order would also be better for partial application or currying.
//: ( Array Int, Array Int ) -> Array Int
const destroyer = (nopes, arr) =>
arr.filter(value => !nopes.includes(value))
destroyer([2, 3], [1, 2, 3, 1, 2, 3])
Use Array#indexOf
to check if the element exists in the arguments
const destroyer = (arr, ...args) => arr.filter(value => args.indexOf(value) === -1);
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3);
This is my solution with a bit of explanation. It is similar to the others with subtle differences. It is more detailed showing what the callback function does. Hope it helps understand it for everyone!
let arr = [1, 2, 3, 3, 4, 5]
function destroyer(myArray) {
let argsArr = Array.from(arguments) //argsArr = [[1,2,3,3,4,5],3,4]
let sliced = argsArr.slice.call(arguments, 1) //sliced = [3,4]
//myArray = [1,2,3,3,4,5]
function takeAway(val) { // val = each element in myArray:1,2,3,3,4,5
return sliced.indexOf(val) === -1 //[3,4] indexOf 3,4 = true (+1) [3,4] indexOf 1,2,5
// == false (-1) so it is returned by the filter method
}
let answer = myArray.filter(takeAway) // answer = [1,2,5]
return answer
}
console.log(destroyer(arr, 3, 4))