I am working on a .filter() method and a given solution has a .include() method in it. I was trying to solve the problem using an inner loop, next I switched to .filter and attempted using an if statement in the inner function.
Below is a code sample:
let arrayOne = [1,2,3,4,5,6];
let arrayTwo = [2,4,6,8,10,'water'];
const filterComparism=(arr1,arr2)=>{
let newArray=[];
/**return arr1.filter(item=>arr2.includes(item)); this line of Code would solve the issue. is there an alternative step to solve below?
*/
for(let i = 0; i<arr1.length; i++){
for(let j = 0; j< arr2.length; j++){
newArray.push(arr1===arr2? return true);
}
}
return newArray;
}
console.log(filterComparism(arrayOne,arrayTwo));
const filterComparism=(arr1,arr2)=>{
/**return arr1.filter(item=>arr2.includes(item)); this line of Code would solve the issue. is there an alternative step to solve below?
*/
return arr1.filter(item=>{
if(item===arr2){
return true;
}
return false;
});
}
is there an alternative method to achieve this using an if statement in the inner function or using inner loops to compare values.