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?
Given a sorted array, the task is to remove the duplicate elements from the array. Create an auxiliary array temp[] to store unique elements. Traverse input array and one by one copy unique elements of arr[] to temp[]. Also keep track of count of unique elements.
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)
.
jsFiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With