Subtracting elements from two arrays.
arr1=["AT","IN","UK","US"]
and arr2=["UK","ZA"]
. I want the result to be arr1-arr2 = ["AT","IN","US"]
I have attempted to perform the subtraction of the arrays as described above, but instead of obtaining the correct output, I keep getting arr1[]
. Here is the code I used:
var firstArray = ["AT","IN","UK","US"];
var secondArray= ["UK","ZA"] ;
var subtractedArray = [];
subtractedArray = firstArray ;
console.log(subtractedArray);
for(i=0;i<firstArray.length;i++)
{
for(j=0;j<=secondArray.length;j++)
{
if (firstArray.includes(secondArray[j]))
{
var index = subtractedArray.indexOf(secondArray[j]);
if (index > -1) {
subtractedArray.splice(index, 1);
}
}
}
}