Distinct array of variable

159 Views Asked by At

My question is simple. The array is claimed by a variable.

var array = [1,1,2,2,2,3,3,,4];

How can I get the new array like

array2 = [1,2,3,4] 
1

There are 1 best solutions below

1
On

This might help (Unique values in an array). This answer is from above link

function onlyUnique(value, index, self) {
    return self.indexOf(value) === index;
}

usage example:

var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter( onlyUnique ); // returns ['a', 1, 2, '1']