Filter out array to have only unique values
up vote
11
down vote
favorite
I need to filter out my array to contain only unique values.
this is my array 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"]
expected result should be
["X_row7", "X_row4", "X_row6", "X_row10", "X_row11", "X_row8"]
How should i continue my code to get proper result.
newArray = ;
for(n in data){
if(!newArray.indexOf(n)){
newArray.push(n);
}
}
console.log(newArray);
If you need any additional information's please let me know and i will provide. thank you
javascript arrays
add a comment |
up vote
11
down vote
favorite
I need to filter out my array to contain only unique values.
this is my array 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"]
expected result should be
["X_row7", "X_row4", "X_row6", "X_row10", "X_row11", "X_row8"]
How should i continue my code to get proper result.
newArray = ;
for(n in data){
if(!newArray.indexOf(n)){
newArray.push(n);
}
}
console.log(newArray);
If you need any additional information's please let me know and i will provide. thank you
javascript arrays
Possible duplicate of Unique values in an array
– Schleis
Jul 5 '16 at 15:10
["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"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
– Pranav C Balan
Jul 5 '16 at 15:11
Possible duplicate of How to get unique values in an array
– Adeel Imran
Dec 25 '17 at 19:34
add a comment |
up vote
11
down vote
favorite
up vote
11
down vote
favorite
I need to filter out my array to contain only unique values.
this is my array 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"]
expected result should be
["X_row7", "X_row4", "X_row6", "X_row10", "X_row11", "X_row8"]
How should i continue my code to get proper result.
newArray = ;
for(n in data){
if(!newArray.indexOf(n)){
newArray.push(n);
}
}
console.log(newArray);
If you need any additional information's please let me know and i will provide. thank you
javascript arrays
I need to filter out my array to contain only unique values.
this is my array 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"]
expected result should be
["X_row7", "X_row4", "X_row6", "X_row10", "X_row11", "X_row8"]
How should i continue my code to get proper result.
newArray = ;
for(n in data){
if(!newArray.indexOf(n)){
newArray.push(n);
}
}
console.log(newArray);
If you need any additional information's please let me know and i will provide. thank you
javascript arrays
javascript arrays
asked Jul 5 '16 at 15:08
Valor_
97822157
97822157
Possible duplicate of Unique values in an array
– Schleis
Jul 5 '16 at 15:10
["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"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
– Pranav C Balan
Jul 5 '16 at 15:11
Possible duplicate of How to get unique values in an array
– Adeel Imran
Dec 25 '17 at 19:34
add a comment |
Possible duplicate of Unique values in an array
– Schleis
Jul 5 '16 at 15:10
["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"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
– Pranav C Balan
Jul 5 '16 at 15:11
Possible duplicate of How to get unique values in an array
– Adeel Imran
Dec 25 '17 at 19:34
Possible duplicate of Unique values in an array
– Schleis
Jul 5 '16 at 15:10
Possible duplicate of Unique values in an array
– Schleis
Jul 5 '16 at 15:10
["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"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
– Pranav C Balan
Jul 5 '16 at 15:11
["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"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
– Pranav C Balan
Jul 5 '16 at 15:11
Possible duplicate of How to get unique values in an array
– Adeel Imran
Dec 25 '17 at 19:34
Possible duplicate of How to get unique values in an array
– Adeel Imran
Dec 25 '17 at 19:34
add a comment |
10 Answers
10
active
oldest
votes
up vote
35
down vote
accepted
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 );
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)]
2
this is brilliant
– oMiKeY
Nov 15 '17 at 20:10
add a comment |
up vote
13
down vote
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"]
add a comment |
up vote
5
down vote
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);
add a comment |
up vote
3
down vote
You can use Map and Spread Operator:
var rawData = ["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 unique = new Map();
rawData.forEach(d => unique.set(d, d));
var uniqueItems = [...unique.keys()];
console.log(uniqueItems);
add a comment |
up vote
3
down vote
arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];
uniqueArr = [... new Set(arr)];
// or
reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)
console.log(`${uniqueArr}n${reallyUniqueArr}`)
1
Please provide context to the code
– Clíodhna
Sep 7 '17 at 9:48
add a comment |
up vote
2
down vote
You can use reduce
to loop the array and get the not duplicate values. Also uses an aux object
to get the count of added values.
var aux = {};
var newArray = ["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"].reduce((tot, curr)=>{
if(!aux[curr]){
aux[curr] = 1;
tot.push(curr);
}
return tot;
}, );
console.log(newArray);
add a comment |
up vote
2
down vote
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);
add a comment |
up vote
1
down vote
A slight variation on the indexOf method, if you need to filter multiple arrays:
function unique(item, index, array) {
return array.indexOf(item) == index;
}
Use as such:
arr.filter(unique);
add a comment |
up vote
1
down vote
I've always used:
unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);
But recently I had to get unique values for:
["1", 1, "2", 2, "3", 3]
And my old standby didn't cut it, so I came up with this:
uunique = (arr) => Object.keys(Object.assign({}, ...arr.map(a=>({[a]:true}))));
add a comment |
up vote
1
down vote
Array.prototype.unique = function () {
return [...new Set(this)]
}
then we can write:
const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
const uniqueArr = arr.unique()
add a comment |
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
35
down vote
accepted
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 );
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)]
2
this is brilliant
– oMiKeY
Nov 15 '17 at 20:10
add a comment |
up vote
35
down vote
accepted
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 );
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)]
2
this is brilliant
– oMiKeY
Nov 15 '17 at 20:10
add a comment |
up vote
35
down vote
accepted
up vote
35
down vote
accepted
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 );
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 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 );
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)]
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 );
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 );
edited Nov 21 '17 at 12:16
answered Jul 5 '16 at 15:11
Mohit Bhardwaj
5,95611439
5,95611439
2
this is brilliant
– oMiKeY
Nov 15 '17 at 20:10
add a comment |
2
this is brilliant
– oMiKeY
Nov 15 '17 at 20:10
2
2
this is brilliant
– oMiKeY
Nov 15 '17 at 20:10
this is brilliant
– oMiKeY
Nov 15 '17 at 20:10
add a comment |
up vote
13
down vote
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"]
add a comment |
up vote
13
down vote
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"]
add a comment |
up vote
13
down vote
up vote
13
down vote
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"]
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"]
edited Apr 27 '17 at 20:00
answered Apr 27 '17 at 18:55
Travis Heeter
4,40424068
4,40424068
add a comment |
add a comment |
up vote
5
down vote
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);
add a comment |
up vote
5
down vote
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);
add a comment |
up vote
5
down vote
up vote
5
down vote
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);
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);
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);
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);
edited Nov 11 '17 at 15:45
answered Jul 5 '16 at 15:11
Nina Scholz
170k1383147
170k1383147
add a comment |
add a comment |
up vote
3
down vote
You can use Map and Spread Operator:
var rawData = ["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 unique = new Map();
rawData.forEach(d => unique.set(d, d));
var uniqueItems = [...unique.keys()];
console.log(uniqueItems);
add a comment |
up vote
3
down vote
You can use Map and Spread Operator:
var rawData = ["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 unique = new Map();
rawData.forEach(d => unique.set(d, d));
var uniqueItems = [...unique.keys()];
console.log(uniqueItems);
add a comment |
up vote
3
down vote
up vote
3
down vote
You can use Map and Spread Operator:
var rawData = ["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 unique = new Map();
rawData.forEach(d => unique.set(d, d));
var uniqueItems = [...unique.keys()];
console.log(uniqueItems);
You can use Map and Spread Operator:
var rawData = ["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 unique = new Map();
rawData.forEach(d => unique.set(d, d));
var uniqueItems = [...unique.keys()];
console.log(uniqueItems);
var rawData = ["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 unique = new Map();
rawData.forEach(d => unique.set(d, d));
var uniqueItems = [...unique.keys()];
console.log(uniqueItems);
var rawData = ["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 unique = new Map();
rawData.forEach(d => unique.set(d, d));
var uniqueItems = [...unique.keys()];
console.log(uniqueItems);
answered Jul 5 '16 at 17:23
Morteza Tourani
2,71942638
2,71942638
add a comment |
add a comment |
up vote
3
down vote
arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];
uniqueArr = [... new Set(arr)];
// or
reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)
console.log(`${uniqueArr}n${reallyUniqueArr}`)
1
Please provide context to the code
– Clíodhna
Sep 7 '17 at 9:48
add a comment |
up vote
3
down vote
arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];
uniqueArr = [... new Set(arr)];
// or
reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)
console.log(`${uniqueArr}n${reallyUniqueArr}`)
1
Please provide context to the code
– Clíodhna
Sep 7 '17 at 9:48
add a comment |
up vote
3
down vote
up vote
3
down vote
arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];
uniqueArr = [... new Set(arr)];
// or
reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)
console.log(`${uniqueArr}n${reallyUniqueArr}`)
arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];
uniqueArr = [... new Set(arr)];
// or
reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)
console.log(`${uniqueArr}n${reallyUniqueArr}`)
arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];
uniqueArr = [... new Set(arr)];
// or
reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)
console.log(`${uniqueArr}n${reallyUniqueArr}`)
arr = ["I", "do", "love", "JavaScript", "and", "I", "also", "do", "love", "Java"];
uniqueArr = [... new Set(arr)];
// or
reallyUniqueArr = arr.filter((item, pos, ar) => ar.indexOf(item) === pos)
console.log(`${uniqueArr}n${reallyUniqueArr}`)
answered Sep 7 '17 at 9:42
Jesse Dirisu
312
312
1
Please provide context to the code
– Clíodhna
Sep 7 '17 at 9:48
add a comment |
1
Please provide context to the code
– Clíodhna
Sep 7 '17 at 9:48
1
1
Please provide context to the code
– Clíodhna
Sep 7 '17 at 9:48
Please provide context to the code
– Clíodhna
Sep 7 '17 at 9:48
add a comment |
up vote
2
down vote
You can use reduce
to loop the array and get the not duplicate values. Also uses an aux object
to get the count of added values.
var aux = {};
var newArray = ["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"].reduce((tot, curr)=>{
if(!aux[curr]){
aux[curr] = 1;
tot.push(curr);
}
return tot;
}, );
console.log(newArray);
add a comment |
up vote
2
down vote
You can use reduce
to loop the array and get the not duplicate values. Also uses an aux object
to get the count of added values.
var aux = {};
var newArray = ["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"].reduce((tot, curr)=>{
if(!aux[curr]){
aux[curr] = 1;
tot.push(curr);
}
return tot;
}, );
console.log(newArray);
add a comment |
up vote
2
down vote
up vote
2
down vote
You can use reduce
to loop the array and get the not duplicate values. Also uses an aux object
to get the count of added values.
var aux = {};
var newArray = ["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"].reduce((tot, curr)=>{
if(!aux[curr]){
aux[curr] = 1;
tot.push(curr);
}
return tot;
}, );
console.log(newArray);
You can use reduce
to loop the array and get the not duplicate values. Also uses an aux object
to get the count of added values.
var aux = {};
var newArray = ["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"].reduce((tot, curr)=>{
if(!aux[curr]){
aux[curr] = 1;
tot.push(curr);
}
return tot;
}, );
console.log(newArray);
var aux = {};
var newArray = ["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"].reduce((tot, curr)=>{
if(!aux[curr]){
aux[curr] = 1;
tot.push(curr);
}
return tot;
}, );
console.log(newArray);
var aux = {};
var newArray = ["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"].reduce((tot, curr)=>{
if(!aux[curr]){
aux[curr] = 1;
tot.push(curr);
}
return tot;
}, );
console.log(newArray);
answered Jul 5 '16 at 15:16
Jose Hermosilla Rodrigo
2,04411027
2,04411027
add a comment |
add a comment |
up vote
2
down vote
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);
add a comment |
up vote
2
down vote
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);
add a comment |
up vote
2
down vote
up vote
2
down vote
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);
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);
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);
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);
answered Feb 10 at 23:57
JSON C11
3,38443537
3,38443537
add a comment |
add a comment |
up vote
1
down vote
A slight variation on the indexOf method, if you need to filter multiple arrays:
function unique(item, index, array) {
return array.indexOf(item) == index;
}
Use as such:
arr.filter(unique);
add a comment |
up vote
1
down vote
A slight variation on the indexOf method, if you need to filter multiple arrays:
function unique(item, index, array) {
return array.indexOf(item) == index;
}
Use as such:
arr.filter(unique);
add a comment |
up vote
1
down vote
up vote
1
down vote
A slight variation on the indexOf method, if you need to filter multiple arrays:
function unique(item, index, array) {
return array.indexOf(item) == index;
}
Use as such:
arr.filter(unique);
A slight variation on the indexOf method, if you need to filter multiple arrays:
function unique(item, index, array) {
return array.indexOf(item) == index;
}
Use as such:
arr.filter(unique);
answered Nov 15 '17 at 20:32
oMiKeY
2,91351839
2,91351839
add a comment |
add a comment |
up vote
1
down vote
I've always used:
unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);
But recently I had to get unique values for:
["1", 1, "2", 2, "3", 3]
And my old standby didn't cut it, so I came up with this:
uunique = (arr) => Object.keys(Object.assign({}, ...arr.map(a=>({[a]:true}))));
add a comment |
up vote
1
down vote
I've always used:
unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);
But recently I had to get unique values for:
["1", 1, "2", 2, "3", 3]
And my old standby didn't cut it, so I came up with this:
uunique = (arr) => Object.keys(Object.assign({}, ...arr.map(a=>({[a]:true}))));
add a comment |
up vote
1
down vote
up vote
1
down vote
I've always used:
unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);
But recently I had to get unique values for:
["1", 1, "2", 2, "3", 3]
And my old standby didn't cut it, so I came up with this:
uunique = (arr) => Object.keys(Object.assign({}, ...arr.map(a=>({[a]:true}))));
I've always used:
unique = (arr) => arr.filter((item, i, s) => s.lastIndexOf(item) == i);
But recently I had to get unique values for:
["1", 1, "2", 2, "3", 3]
And my old standby didn't cut it, so I came up with this:
uunique = (arr) => Object.keys(Object.assign({}, ...arr.map(a=>({[a]:true}))));
answered Jan 24 at 6:44
TennisVisuals
16916
16916
add a comment |
add a comment |
up vote
1
down vote
Array.prototype.unique = function () {
return [...new Set(this)]
}
then we can write:
const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
const uniqueArr = arr.unique()
add a comment |
up vote
1
down vote
Array.prototype.unique = function () {
return [...new Set(this)]
}
then we can write:
const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
const uniqueArr = arr.unique()
add a comment |
up vote
1
down vote
up vote
1
down vote
Array.prototype.unique = function () {
return [...new Set(this)]
}
then we can write:
const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
const uniqueArr = arr.unique()
Array.prototype.unique = function () {
return [...new Set(this)]
}
then we can write:
const arr = [1, 5, 2, 2, 2, 3, 4, 3, 2, 1, 5]
const uniqueArr = arr.unique()
answered Nov 19 at 7:20
Zuhair Taha
6921922
6921922
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38206915%2ffilter-out-array-to-have-only-unique-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Unique values in an array
– Schleis
Jul 5 '16 at 15:10
["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"].filter(function(v,i,arr){ return i==arr.indexOf(v); })
– Pranav C Balan
Jul 5 '16 at 15:11
Possible duplicate of How to get unique values in an array
– Adeel Imran
Dec 25 '17 at 19:34