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

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

Let’s say the following are our array elements from the 1st array −

100,110,150,180,190,175]

And the following are our array elements from the 2nd array −

100,150,175

To filter this array from all elements of another array, use filter().

Example

Following is the code −

var result = [100,110,150,180,190,175].filter(
   function(obj) {
      return this.indexOf(obj) < 0;
   },
   [100,150,175]
);
console.log("After filter, the result is=");
console.log(result);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo269.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo269.js
After filter, the result is=
[ 110, 180, 190 ]
raja
Published on 09-Nov-2020 07:47:45
Advertisements