Filter array to have unique values
Filtering an array to contain unique values can be achieved using the JavaScript Set and Array.from method, as shown below:
Array.from(new Set(arrayOfNonUniqueValues));
Set
The Set object lets you store unique values of any type, whether primitive values or object references.
Return value A new Set object.
Array.from()
The Array.from() method creates a new Array instance from an array-like or iterable object.
Return value A new Array instance.
Example Code:
const array = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
const uniqueArray = Array.from(new Set(array));
console.log("uniqueArray: ", uniqueArray);
You can use Array.filter function to filter out elements of an array based on the return value of a callback function. The callback function runs for every element of the original array.
The logic for the callback function here is that if the indexOf
value for current item is same as the index, it means the element has been encountered first time, so it can be considered unique. If not, it means the element has been encountered already, so should be discarded now.
var arr = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"];
var filteredArray = arr.filter(function(item, pos){
return arr.indexOf(item)== pos;
});
console.log( filteredArray );
Caveat: As pointed out by rob in the comments, this method should be avoided with very large arrays as it runs in O(N^2)
.
UPDATE (16 Nov 2017)
If you can rely on ES6 features, then you can use Set object and Spread operator to create a unique array from a given array, as already specified in @Travis Heeter's answer below:
var uniqueArray = [...new Set(array)]
You could use a hash table for look up and filter all not included values.
var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"],
unique = data.filter(function (a) {
return !this[a] && (this[a] = true);
}, Object.create(null));
console.log(unique);
As of June 15, 2015 you may use Set()
to create a unique array:
var uniqueArray = [...new Set(array)]
For your Example:
var data = ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11", "X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]
var newArray = [...new Set(data)]
console.log(newArray)
>> ["X_row7", "X_row4", "X_row6", "X_row10", "X_row8", "X_row9", "X_row11"]