- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the difference between two arrays in JavaScript?
To get the difference between two arrays in JavaScript, try to run the following code. Here, we’re using some method like split(), indexOf(), sort(), etc to get the elements, which aren’t the same in both the arrays &mnus;
Example
<html> <head> <title>JavaScript Dates</title> </head> <body> <script> function arrDifference (arr1, arr2) { var arr = []; arr1 = arr1.toString().split(',').map(Number); arr2 = arr2.toString().split(',').map(Number); // for array1 for (var i in arr1) { if(arr2.indexOf(arr1[i]) === -1) arr.push(arr1[i]); } // for array2 for(i in arr2) { if(arr1.indexOf(arr2[i]) === -1) arr.push(arr2[i]); } return arr.sort((x,y) => x-y); } document.write(arrDifference([50, 40, 90], [70, 50, 99, 40, 90])); </script> </body> </html>
Output
70,99
- Related Articles
- Finding the difference between two arrays - JavaScript
- Find the Symmetric difference between two arrays - JavaScript
- How to write a JavaScript function to get the difference between two numbers?
- How to find set difference between two Numpy arrays?
- Get the property of the difference between two objects in JavaScript
- How to get the difference between two dates in Android?
- Find the compatibility difference between two arrays in C++
- How to calculate the difference between two dates in JavaScript?
- How to find the common elements between two or more arrays in JavaScript?
- How to get time difference between two timestamps in seconds?
- How to merge two arrays in JavaScript?
- How to join two arrays in JavaScript?
- How to multiply two Arrays in JavaScript?
- How to get the intersection of two arrays in MongoDB?
- How to get substring between two similar characters in JavaScript?

Advertisements