0

This feels like a very basic javascript question but I have two arrays:

var arrayA =['content','I dont','want'];
var arrayB = ['content','want','puppies'];

desired result:

arrayB = ['puppies']

The items in the arrays are strings.

How do I do this? (bonus points if the answer works in IE8+)

4
  • What have you tried? Perhaps a good starting point is how do you delete something from an array? Commented Apr 13, 2017 at 20:40
  • Why in arrayB's element is puppies here. I mean what kind of operation actually happens here. Commented Apr 13, 2017 at 20:40
  • what's supposed to happen to "I dont" here? Commented Apr 13, 2017 at 20:47
  • I understand how to remove one singular item from an array, was just hoping there was an easier way than looping through each item in an array and then checking index of that item in another array, then removing it if it's there. Just seemed like a painful way to do something I'd guess there was a built in function intended for. Also the text in the strings was purely an example could have as easily used "foo" "bar". I was trying to boil this down to it's simplest form as the full thing I'm building is way more complex. I just forgot how to do this and couldn't find a useful answer googling Commented Apr 13, 2017 at 21:13

7 Answers 7

4

You could filter arrayB and take only the elements, which are not included in arrayA

var arrayA =['content','I dont','want'],
    arrayB = ['content','want','puppies'].filter(a => !arrayA.includes(a));

console.log(arrayB);

ES5

var arrayA =['content','I dont','want'],
    arrayB = ['content','want','puppies'].filter(function (a) {
        return arrayA.indexOf(a) === -1;
    });

console.log(arrayB);

Sign up to request clarification or add additional context in comments.

Comments

1

remove all items in arrayA from arrayB

The solution using Array.prototype.forEach() and Array.prototype.splice() functions:

var arrayA =['content','I dont','want'],
    arrayB = ['content','want','puppies'];

arrayA.forEach(function (w) {
    var idx = arrayB.indexOf(w);
    ~idx && arrayB.splice(idx, 1);
});

console.log(arrayB);

Comments

1

For value types, the following should work: (alas not in IE8 - as pointed out!)

function (arrayA, arrayB) {
  return arrayB.filter(function(b) {
    return arrayA.indexOf(b) == -1;
  });
}

3 Comments

That won't work in IE8. IE8 doesn't support filter or indexOf
@jas7457 I think I really knew that about filter deep down. Although indexOf() support lacking in IE8 was genuinely news to me! Learn something new everyday!
You can add indexOf to IE8 stackoverflow.com/questions/3629183/…
0

This will get you down to IE9, but IE8 doesn't support indexOf. If you absolutely need IE8 support, you'll have to write a polyfill for indexOf.

var arrayA =['content','I dont','want'];
var arrayB = ['content','want','puppies'];

for(var i = 0; i < arrayA.length; i++){
    var index = arrayB.indexOf(arrayA[i]);
  if(index !== -1) {
    arrayB.splice(index, 1);
  }
}

console.log(arrayB);

Comments

0
arrayA.forEach((valueA)=>{
    let idx = (arrayB.findIndex( (valueB)=>valueB === valueA))
    if(idx != -1){
      arrayB.splice(idx,1)
    }
})

Comments

0

This will get you to IE8

var arrayA =['content','I dont','want'];
var arrayB = ['content','want','puppies'];

for (var i = arrayB.length - 1; i >= 0; i--) {
    for (var j = arrayA.length - 1; j >= 0; j--) {
        if (arrayA[j] == arrayB[i]) {
            arrayB.splice(i, 1);
        }
    }
}

console.log(arrayB);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.