Skip to content Skip to sidebar Skip to footer

Remove Multiple Items From An Array Matching A String In Javascript

I have two arrays and I want to remove items from arr which are in removeStr: var arr = [ '8','abc','b','c']; // This array contains strings that needs to be removed from main arr

Solution 1:

In that case, I think you would need to loop through the removeStr array, and check if each arr element contains the string in the removeStr array.

// var arr = ['8', 'abc', 'b', 'c'];
    // // This array contains strings that needs to be removed from main array
    // var removeStr = ['abc', '8'];

    var arr = ['abc / **efg**', 'hij / klm', '**nop** / qrs', '**efg** / okl'];

    var removeStr = ['efg', 'nop'];

    arr = arr.filter(function (val) {
        var found = false;
        for (var i = 0; i < removeStr.length; i++) {
            var str = removeStr[i];
            if (val.indexOf(str) > -1) {
                return false;
            }
        }
        return true;
    });

    console.log(arr);

    // 'arr' Outputs to :
    ['b', 'c']

Post a Comment for "Remove Multiple Items From An Array Matching A String In Javascript"