How to get the elements of one array which are not present in another array using JavaScript?
The task is to get the elements of the one array which are not present in the another array. Here we are going to use JavaScript to achieve the goal. Here are few techniques discussed.
Approach
- Take the arrays in variables.
- Use the .filter() method on the first array and check if the elements of first array are not present in the second array, Include those elements in the output.
Example 1: This example using the approach discussed above.
<!DOCTYPE HTML> <html> <head> <title> How to get the elements of one array which are not present in another array using JavaScript? </title> </head> <body style="text-align:center;" id="body"> <h1 id="h1" style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> Click here </button> <p id="GFG_DOWN" style="font-size: 23px; font-weight: bold; color: green; "> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var ar1 = [1, 2, 3, 4]; var ar2 = [2, 4]; el_up.innerHTML = "Click on the button to remove the element of "+ "second from the first array.<br><br> firstArray - " + ar1 + "<br>secondArray - " + ar2; function gfg_Run() { var elmts = ar1.filter( function(i) { return this.indexOf(i) < 0; }, ar2 ); el_down.innerHTML = elmts; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example using the same approach but different methods of javaScript.
<!DOCTYPE HTML> <html> <head> <title> How to get the elements of one array which are not present in another array using JavaScript? </title> </head> <body style="text-align:center;" id="body"> <h1 id="h1" style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> Click here </button> <p id="GFG_DOWN" style="font-size: 23px; font-weight: bold; color: green; "> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var ar1 = [1, 2, 3, 4]; var ar2 = [2, 4]; el_up.innerHTML = "Click on the button to remove the element of second"+ " from the first array.<br><br> firstArray - " + ar1 + "<br>secondArray - " + ar2; function gfg_Run() { var elmts = ar1.filter(f => !ar2.includes(f)); el_down.innerHTML = elmts; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to store all dates in an array present in between given two dates in JavaScript ?
- Remove elements from a JavaScript Array
- JavaScript | Add new elements at the beginning of an array
- Sum and Product of Array elements using JavaScript
- How to remove multiple elements from array in JavaScript ?
- JavaScript | Remove empty elements from an array
- How to unpack array elements into separate variables using JavaScript ?
- How to find the index of all occurrence of elements in an array using JavaScript ?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- How to check whether an array is subset of another array using JavaScript ?
- Delete the array elements in JavaScript | delete vs splice
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- Display the number of links present in a document using JavaScript
- JavaScript | Array pop()
- RMS Value Of Array in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


