-2

I have one array like var data= [1,1,3,4,2,4,5,6,7,5,4] and I need the unique array of data without a loop or any Lodash and underscore js function

Vatsal Dashani
  • 75
  • 1
  • 11
  • `console.log([...new Set(data)]);`....? http://jsfiddle.net/1wk9mxbq/ May you please show your attempt? – briosheje Oct 15 '18 at 12:07

3 Answers3

2

Use a Set

 var data= [1,1,3,4,2,4,5,6,7,5,4] ;
 const unique = [...new Set(data)];
 console.log(unique); 
baao
  • 71,625
  • 17
  • 143
  • 203
  • If you want ES5, you can use `data.filter((value, index, array) => array.indexOf(value) === index);` – lloydaf Oct 15 '18 at 12:42
1

for ES6/ES2015: Using the Set, the single line solution is:

var data= [1,1,3,4,2,4,5,6,7,5,4] ;
var uniqueItems = Array.from(new Set(data);

Or shortened using spread operator

var uniqueItems = [...new Set(data)];
Osama
  • 2,912
  • 1
  • 12
  • 15
0

You can use uniq() of Underscore.js as below. This also have sort option.

import * as _ from 'underscore';

var data= [1,1,3,4,2,4,5,6,7,5,4];
var unique = _.uniq(data);
Harun Or Rashid
  • 5,589
  • 1
  • 19
  • 21