I am trying the nested array, can you help me with the loop, the code is not optimized and i need some guidance. I have 2 arrays, i am not getting the expected results.
let arr1 = ['a','b','c','d','e'];
let arr2 = ['b','e','f'];
var temp = arr2
for(i =0; i< arr1.length; i++){
for(j=0; j<arr2.length; j++){
var flag = false;
if(arr1[i] === arr2[j]){
flag = true;
}
if(arr2.length -1 === j){
if(flag == false){
temp.push(arr1[i])
}
if( arr1.length - 1 == i){
console.log(temp)
}
}
}
}
What i am trying to achieve is, from the second array i want output in this format
temp = ['b','e','f','a','c','d']
The elements which are not present in arr1 should be pushed to arr2 elements. I apologize for the beginner's code. I am still learning. Thank you.
tempis a pointer toarr2, so changes totempare changes toarr2. to avoid that, use:temp=arr2.slice(), which will make a copy, instead.