Remove all elements from array that match specific string

Simply use the Array.prototype.filter() function for obtain elements of a condition

var array = [1,2,'deleted',4,5,'deleted',6,7];
var newarr = array.filter(function(a){return a !== 'deleted'})

Update: ES6 Syntax

let array = [1,2,'deleted',4,5,'deleted',6,7]
let newarr = array.filter(a => a !== 'deleted')

If you have multiple strings to remove from main array, You can try this

// Your main array 
var arr = [ '8','abc','b','c'];

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

arr = arr.filter(function(val){
  return (removeStr.indexOf(val) == -1 ? true : false)
})

console.log(arr);

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

OR

Better Performance(Using hash) , If strict type equality not required

// Your main array 
var arr = [ '8','deleted','b','c'];

// This array contains strings that needs to be removed from main array
var removeStr = [ 'deleted' , '8'];
var removeObj = {};  // Use of hash will boost performance for larger arrays
removeStr.forEach( e => removeObj[e] = true);

var res = arr.filter(function(val){
  return !removeObj[val]
})

console.log(res);

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

Tags:

Javascript