var arr1 = [\u2018a\u2019, \u2018b\u2019, \u2018c\u2019, \u2018d\u2019];\n\nvar arr2 = [\u2018b\u2019, \u2018c\u2019];\n\narr1.filter(function(element) {\n\n return !arr2.includes(element); \/\/ include only those elements which are not present in arr2\n\n}).forEach(function(element) { \/\/ print only those elements which are not present in arr2\n\n console.log(element);\n\n});<\/code><\/pre><\/div>\n\n\n\nOutput<\/strong>:<\/p>\n\n\n\na<\/p>\n\n\n\n
d<\/p>\n\n\n\n
Set Object<\/h3>\n\n\n\n
You can convert one array into Set object and then use the difference() method to find the difference between two arrays in JavaScript.<\/p>\n\n\n\n
Example:<\/p>\n\n\n\n
var arr1 = [\u2018a\u2019, \u2018b\u2019, \u2018c\u2019, \u2018d\u2019];\n\nvar arr2 = [\u2018b\u2019, \u2018c\u2019];\n\nvar set2 = new Set(arr2);\n\nvar difference = [...arr1].filter(x => !set2.has(x))); \/\/ find elements which are present in set1, but not in set2\n\nconsole.log(Array.from(difference)); \/\/ convert the difference back to an array<\/code><\/pre><\/div>\n\n\n\nOutput<\/strong>:<\/p>\n\n\n\n[“a”, “d”]<\/p>\n\n\n\n
jQuery methods<\/h3>\n\n\n\n
If you are already using jQuery in your project, then you can use the $.grep() and $.inArray() methods to find the difference between two arrays in JavaScript.<\/p>\n\n\n\n
Example:<\/p>\n\n\n\n
var arr1 = [\u2018a\u2019, \u2018b\u2019, \u2018c\u2019, \u2018d\u2019];\n\nvar arr2 = [\u2018b\u2019, \u2018c\u2019];\n\n$.grep(arr1, function(element) {\n\n return $.inArray(element, arr2 ) < 0; \/\/ include only those elements which are not present in arr2\n\n}).forEach(function(element) { \/\/ print only those elements which are not present in arr2\n\n console.log(element);\n\n});<\/code><\/pre><\/div>\n\n\n\nOutput<\/strong>:<\/p>\n\n\n\na<\/p>\n\n\n\n
d<\/p>\n\n\n\n
Underscore.js methods<\/h3>\n\n\n\n
If you are already using Underscore.js in your project, then you can use the difference() method to find the difference between two arrays in JavaScript. The difference() method returns the values from arr1 which are not present in arr2.<\/p>\n\n\n\n
Example:<\/p>\n\n\n\n
var arr1 = [\u2018a\u2019, \u2018b\u2019, \u2018c\u2019, \u2018d\u2019];\n\nvar arr2 = [\u2018b\u2019, \u2018c\u2019];\n\nconsole.log(_.difference(arr1, arr2)); \/\/ find elements which are present in arr1, but not in arr2<\/code><\/pre><\/div>\n\n\n\nOutput<\/strong>:<\/p>\n\n\n\n[“a”, “d”]<\/p>\n\n\n\n
Conclusion<\/h2>\n\n\n\n
As you can see from the above examples, there are many ways to find the difference between two arrays in JavaScript. Which method you choose will depend on the requirements of your project.<\/p>\n\n\n\n
I hope you found this article helpful. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"
To find the difference between two arrays in JavaScript, you can use the following methods – Array.prototype.filter() and indexOf() method Array.prototype.filter() and includes() method Set object jQuery methods Underscore\/Lodash library Find The Difference Between Two Arrays In JavaScript Let’s look at these methods one by one, but let’s first start by understanding what difference means […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[12],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts\/486"}],"collection":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/comments?post=486"}],"version-history":[{"count":5,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":1966,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts\/486\/revisions\/1966"}],"wp:attachment":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/media?parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/categories?post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/tags?post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}