Home > Mobile >  Filtering an array from another array in vue
Filtering an array from another array in vue

Time:12-09

This might be something really simple, but I just can't figure it out. What I'm trying to do is take 2 arrays and filter out what I don't need and only return the one array.

So what I have right now is this

let array1 = [1, 2, 3];
let array2 = [1, 2, 3, 4, 5, 6];

and what I would like is to return array 2 with only the items that doesn't show up in array1 so that would be 4, 5,6.

This is what I have so far


return array1.forEach(a => {
    array2.filter(aa => aa !== a)
});

and that doesn't return anything

CodePudding user response:

let array1 = [1, 2, 3];
let array2 = [1, 2, 3, 4, 5, 6];

let array3 = array2.filter(i => !array1.includes(i));

console.log(array3)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

This might help to solve your problem.

 let array1 = [1, 2, 3]
    let array2 = [1, 2, 3, 4, 5, 6]
    function returnList(arOne,arTwo){
      return arTwo.filter(a => !arOne.includes(a))
    }
    let response = returnList(array1 ,array2 );
  • Related