Get all unique values in a JavaScript array (remove duplicates)
With JavaScript 1.6 / ECMAScript 5 you can use the native filter
method of an Array in the following way to get an array with unique values:
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
// usage example:
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);
console.log(unique); // ['a', 1, 2, '1']
How to get unique values in an array
Since I went on about it in the comments for @Rocket's answer, I may as well provide an example that uses no libraries. This requires two new prototype functions, contains
and unique
Array.prototype.contains = function(v) { for (var i = 0; i < this.length; i++) { if (this[i] === v) return true; } return false;};
Array.prototype.unique = function() { var arr = []; for (var i = 0; i < this.length; i++) { if (!arr.contains(this[i])) { arr.push(this[i]); } } return arr;}
var duplicates = [1, 3, 4, 2, 1, 2, 3, 8];var uniques = duplicates.unique(); // result = [1,3,4,2,8]
console.log(uniques);
Get unique values from array of arrays
You can use .flat()
to flatten your array and then use Set
to get its unique values.
Demo:
let arr = [ [ "s1@example.com", "s2@example.com" ], [ "s1@example.com", "s3@example.com" ]]
let arr2 = [...new Set(arr.flat(1))];console.log(arr2)
How to get distinct values from an array of objects in JavaScript?
If this were PHP I'd build an array with the keys and take array_keys
at the end, but JS has no such luxury. Instead, try this:
var flags = [], output = [], l = array.length, i;
for( i=0; i<l; i++) {
if( flags[array[i].age]) continue;
flags[array[i].age] = true;
output.push(array[i].age);
}
Fastest way to get ONLY unique values from array?
The first idea, we can do over two step:
Step1: Sort the array
-- There are many algorithms to do it. As I know, currently, the complexity of best algorithm now is O(Nlog(N)) with N is the number of array.
Step2: Remove the duplicated elements
-- The complexity of this step is O(N)
So, over two steps, the complexity is O(N) + O(Nlog(N)). Finally, the complexity is O(Nlog(N))The second idea
This also has the complexity is O(Nlog(N)) but it will be O(N) for next time you want to get the unique age.
Instead of saving the data in array, you can rebuild in a binary search tree with a little custom. This node in this tree will save all the elements with same age.
The complexity for the first time you build the tree is O(Nlog(N))
About the algorithm which has the complexity is O(N), currently, I think there are no technique to solve it. :D
How do I get unique elements in this array?
You can just use the method uniq
. Assuming your array is ary
, call:
ary.uniq{|x| x.user_id}
and this will return a set with unique user_id
s.
Get the unique values from two arrays and put them in another array
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.
Related Topics
Ruby: How to Remove Items from Array a If It's Not in Array B
How to Require Activerecord in Irb
Ruby on Rails - Helper Method - Undefined Method 'Log_In' in Ruby on Rails
Heroku Deplyoment Asset Precompiling Failed on Rails 6
Importing CSV as Test Data in Cucumber
Rails 3 Translations Within Models in Production
Devise 'Find_First_By_Auth_Conditions' Method Explanation
Nomethoderror on Section 5.7 of Rails Guide
How to Marshal a Hash with Arrays
Vps Apache Config - Invalid Command 'Passengerdefaultruby' After Adding Latest Passenger Gem
Instantiate Capybara Browser and Set a Proxy
How to Convert a Large Gem to Standalone Rails App
Override Vagrant Configuration Settings Locally (Per-Dev)
Why Would We Put a Module Inside a Class in Ruby
How to Add 'Each' Method to Ruby Object (Or Should I Extend Array)
Could Not Find 'Cocoapods' (>= 0.A) Among 48 Total Gem(S) (Gem::Missingspecerror)