close

How to filter an array from all elements of another array

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to filter an array from all elements of another array in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to filter an array from all elements of another array ?

  1. How to filter an array from all elements of another array ?

    to filter an array from all elements of another array There are many answers for your question, but I don't see anyone using lambda expresion:
    var array = [1,2,3,4]; var anotherOne = [2,4]; var filteredArray = array.filter(x => anotherOne.indexOf(x) < 0);

  2. How to filter an array from all elements of another array ?

    to filter an array from all elements of another array There are many answers for your question, but I don't see anyone using lambda expresion:
    var array = [1,2,3,4]; var anotherOne = [2,4]; var filteredArray = array.filter(x => anotherOne.indexOf(x) < 0);

Method 1

If you need to compare an array of objects, this works in all cases:

let arr = [{ id: 1, title: "title1" },{ id: 2, title: "title2" }]
let brr = [{ id: 2, title: "title2" },{ id: 3, title: "title3" }]

const res = arr.filter(f => brr.some(item => item.id === f.id));
console.log(res);

Method 2

There are many answers for your question, but I don’t see anyone using lambda expresion:

var array = [1,2,3,4];
var anotherOne = [2,4];
var filteredArray = array.filter(x => anotherOne.indexOf(x) < 0);

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment