I have two JavaScript array's. For every item in first array, I need to check if that items exists in second array and if does, I should remove that item from second array. I do not want to create another array. At the end of the process I should have second array with the correct values. With below code I am not able to get the desired result.
var arr = [1, 2, 3];
var childArr = [1, 2, 3, 4, 51, 2, 3, 5];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < childArr.length; j++) {
if (childArr[j] === arr[i]) {
childArr.splice(childArr[j], 1);
}
}
}
var difference = childArr.filter(function(e) { return arr.indexOf(e) === -1; });childArrwill be decreased by one when you splice it, right? So you should do tojwhat?differenceorwithoutare designed specifically for this. If underscore is available to you!