How to find unique entries from two different arrays in jquery or javascript?

I want to get unique values ​​from two different arrays.

There are two arrays in JavaScript:

<script> var a=new Array; var b=new Array; a={'a','b','c','d','e'} b={'a','d','e','c'} </script> 

I need a conclusion:

 new array => {'a','c','d','e'} 

How to find unique entries from both arrays using the JavaScript prototype function or jQuery function?

+6
source share
5 answers

I do not know if you have the right conditions. The only values ​​for me would be members that appear only once in any array. It seems you want the members present in both arrays (common values) based on your example.

You can use jQuery to handle this. grep() is your friend.

You can do this without jQuery, but I'm not sure if the native filter() and indexOf() methods have better browser support.

 var a = ['a', 'b', 'c', 'd', 'e'], b = ['a', 'd', 'e', 'c']; var common = $.grep(a, function(element) { return $.inArray(element, b) !== -1; }); console.log(common); // ["a", "c", "d", "e"] 

jsFiddle .

+7
source

I think you really wanted to write:

 <script type="text/javascript"> var a = ['a','b','c','d','e']; var b = ['a','d','e','c']; </script> 

In any case, you can sort arrays and get values ​​from values ​​that are not in another, and vice versa, then combine the two sets into one. You seem to be spoiled for choice, so here is a good basic version of JavaScript that should work in most browsers. Using the new features of the latest browsers will probably fail in older browsers.

 // Compares a to b. Returns all the elements in a that are not in b // If c provided, add unique elements to c function getUnique(a, b, c) { var c = c || []; var ta = a.slice().sort(); var tb = b.slice().sort(); var x, y, found = false; for (var i=0, iLen=ta.length; i<iLen; i++) { x = ta.shift(); for (var j=0; j<tb.length && !found; j++) { // j.length changes each loop if (tb[j] == x) { tb.splice(j,1); // Remove match from b found = true; } } if (!found) { c.push(x); // If no match found, store in result } found = false; } return c; } var a = ['a','b','d']; var b = ['b','e']; var d = getUnique(a, b); alert(d); var c = getUnique(b,a,d); alert(d); 

But your comment on the first answer indicates that you need elements that are common to both arrays, which is simpler:

 function getCommon(a, b) { var c = []; var ta = a.slice().sort(); var tb = b.slice().sort(); var t, found; for (var i=0, iLen=ta.length; i<iLen; i++) { t = ta[i]; found = false; for (var j=0, jLen=tb.length; j<jLen && !found; j++) { if (t == tb[j]) { c.push(tb.splice(j,1)); found = true; } } } return c; } alert(getCommon(a, b)); 

You need to figure out what to do with duplicates. In the first case, duplicates will be considered as unique if there is no duplicate in another array. In the above example, duplicates do not matter if they are not duplicated in both arrays.

+2
source

Find the orignal answer: JavaScript array difference

In this case, you can use Set . It is optimized for this kind of operation (union, intersection, difference).

Make sure this applies to your case if it does not allow duplication.

 var a = new JS.Set([1,2,3,4,5,6,7,8,9]); var b = new JS.Set([2,4,6,8]); a.intersection(b);//intersect will give you the common one 
+1
source

I would like to do this operation using associative array support in JavaScript.

 <script> var a=new Array; var b=new Array; a={'a','b','c','d','e'} b={'a','d','e','c'} var uniqueArray = new Array; var tempArray = new Array; var j = 0; for(var i = 0; i < a.length; i++) { if(!tempArray[a[i]]) { tempArray[a[i]] = true; uniqueArray[j++] = a[i]; } } for(i = 0; i < b.length; i++) { if(!tempArray[b[i]]) { tempArray[b[i]] = true; uniqueArray[j++] = b[i]; } } </script> 
+1
source

Like this:

 var a=['a','b','c','d','e']; //Use brackets var b=['a','d','e','c'] var c = a.concat(b).sort(); var uniques = {}; for(var i=0; i<c.length; i++){ uniques[c[i]] = true; } var uniquesArray = []; for(var u in uniques) uniquesArray.push(u) 

UniquesArray now contains only unique values. Hope this helps

+1
source

Source: https://habr.com/ru/post/887425/


All Articles