ES3 solution.
var arrayA = ['content', 'I dont', 'want'];
var arrayB = ['content', 'want', 'puppies'];
for (var bIdx = arrayB.length - 1; bIdx >= 0; bIdx -= 1) {
for (var aIdx = 0; aIdx < arrayA.length; aIdx += 1) {
if (arrayB[bIdx] === arrayA[aIdx]) {
arrayB.splice(bIdx, 1);
break;
}
}
}
console.log(arrayB);
Notice that the outer loop (arrayB) runs in reverse as you will be removing items from the array with Array#splice and it will therefore be re-indexed while you are iterating it, which would mean that you could miss some items if forward iterated.
See the following for some further examples and explanations.
How to remove element from array in forEach loop?
You can happily use functional methods on IE8 (as long as you are not using sparse arrays) provided you load the appropriate shims for the methods that you use.
https://github.com/es-shims/es5-shim
https://github.com/es-shims/es6-shim
https://github.com/es-shims/es7-shim
If it is not essential that you mutate the original array, then you can use the Array#filter concept and replace the old array with a new one.
var arrayA = ['content', 'I dont', 'want'];
var arrayB = ['content', 'want', 'puppies'];
var result = [];
for (var bIdx = 0; bIdx < arrayB.length; bIdx += 1) {
var itemB = arrayB[bIdx];
var found = false;
for (var aIdx = 0; aIdx < arrayA.length; aIdx += 1) {
if (itemB === arrayA[aIdx]) {
found = true;
break;
}
}
if (found !== true) {
result.push(itemB);
}
}
arrayB = result;
console.log(arrayB);
Notice that we can now forward iterate both arrays as they are not being mutated and no re-indexing is occuring.