JavaScript - Remove Array elements that are of the same value as given arguments



You will be provided with an initial array(the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.


Note:
You have to use the arguments object.


Solution 1 Basic approach


function destroyer(arr) {
    var args = Array.from(arguments);
    args.splice(0, 1);
    var target = args;
    console.log(target)
    var result = []
    for (var num of arr) {
        console.log(num)
        if (target.indexOf(num) === -1) {
            result.push(num);
        }
    }
    return result;
}
console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

Output:


[ 1, 1 ]

Solution 2 Filter function


function destroyer(arr) {
    var args = Array.from(arguments);
    args.splice(0, 1);
    var target = args;

    return arr.filter(num => target.indexOf(num) === -1);
}
console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

Output:


[ 1, 1 ]

download android app

Popular javascript Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube