lodash unique array of strings code example

Example 1: javascript array unique values

var arr = [55, 44, 65,1,2,3,3,34,5];
var unique = [...new Set(arr)]

//just  var unique = new Set(arr) wont be an array

Example 2: array unique values javascript

const myArray = ['a', 1, 'a', 2, '1'];
const unique = [...new Set(myArray)]; // ['a', 1, 2, '1']

Example 3: lodash unique array of objects

var users = [
  {id:1,name:'ted'},
  {id:1,name:'ted'},
  {id:1,name:'bob'},
  {id:3,name:'sara'}
];
var uniqueUsersByID = _.uniqBy(users,'id'); //removed if had duplicate id
var uniqueUsers = _.uniqWith(users, _.isEqual);//removed complete duplicates

Example 4: lodash unique array

_.uniq([2, 1, 2]);
// => [2, 1]
 
// using `isSorted`
_.uniq([1, 1, 2], true);
// => [1, 2]
 
// using an iteratee function
_.uniq([1, 2.5, 1.5, 2], function(n) {
  return this.floor(n);
}, Math);
// => [1, 2.5]
 
// using the `_.property` callback shorthand
_.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]

Tags:

Php Example