I'm trying to Pass the values [1, 2, 3, 1, 2, 3] to be removed using the funcion "destroyer" and the values 2, 3 (or even more values ex: 1,3,5.) to be removed from the previous array. Always the first part is an array to remove from and followed by numbers to remove from the array
Here you have the code that I have to solve:
function destroyer(arr) {
// Remove all the values
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Try this approach. It uses the spread operator and the includes() function
... - is a spread operator
function destroyer(arr, ...items) { return arr.filter(i => !items.includes(i)); } let arr = destroyer([1, 2, 3, 1, 2, 3], 2, 3); console.log(arr);
You can access all parameters passed to a function using arguments
variable. Note, this is an array like object but not an array, so you will have to convert it to array. When you do that, your arr
will be the first value as even that is a part of parameters. You can use .slice(1)
to get all values from second element.
function destroyer(arr) { var args = [].slice.call(arguments,1); return arr.filter(function(val){ return args.indexOf(val) < 0 }) } console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
function destroyer(arr) { var args = Array.from(arguments).slice(1); return arr.filter(x=>!args.includes(x)); } console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
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.