I want to get unique values from two different arrays.
Two arrays are as below in JavaScript:
<script>
var a=new Array;
var b=new Array;
a={'a','b','c','d','e'}
b={'a','d','e','c'}
</script>
I want output like:
new array => {'a','c','d','e'}
How can I find unique records from both arrays using JavaScript prototype function or jQuery function?
I don't know if you have the terms correct. Unique values to me would be members which appear only once in either array. It seems you want members that are present in both arrays (common values, or intersection), based on your example.
You can use jQuery to handle this. grep()
is your friend.
You could do this without jQuery, but I'm not sure if the native filter()
and indexOf()
methods have the best 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"]
With underscore it's easy at _.intersection(arr1, arr2)
.
I think you really meant 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 the arrays and get the values from one that aren't in the other and vice versa, then concatenate the two sets into one. You seem spoilt for choice, so here's a nice basic javascript version that should work in most browsers. Using new features of the latest browsers is certain to 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 want the 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 work out what to do with duplicates. In the first case, a duplicates will be treated as unique if there isn't a duplicate in the other array. In the above, duplicates don't matter unless they are duplicated in both arrays.
Find orignal answer : JavaScript array difference
You could use a Set in this case. It is optimized for this kind of operation (union, intersection, difference).
Make sure it applies to your case, once it allows no duplicates.
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
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)
Now uniquesArray contains only unique values. Hope this Helps
I would like to do this operation with the help of 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>
来源:https://stackoverflow.com/questions/5907012/how-to-find-unique-records-from-two-different-array-in-jquery-or-javascript