Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  131] [ 6]  / answers: 1 / hits: 126880  / 11 Years ago, tue, april 9, 2013, 12:00:00

I have two arrays in javascript -:



var array1 = ['12','1','10','19','100'];
var array2 = ['12','10','19'];


I need to a method to get the unique from two arrays and put them in array3
Array3 should be -:



var array3 = ['1','100'];


Thanks for help.


More From » arrays

 Answers
93
var array3 = array1.filter(function(obj) { return array2.indexOf(obj) == -1; });


MDN on Array#filter: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter



Includes a polyfill for older browsers.


[#79008] Monday, April 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mercedez

Total Points: 525
Total Questions: 103
Total Answers: 102

Location: Trinidad and Tobago
Member since Fri, Mar 24, 2023
1 Year ago
;