lodash uniq code example

Example 1: 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 }]

Example 2: Lodash remove duplicates from array

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 3: lodash map

const _quote_filter = _.map(quote_state, (val, key) => {   if (val) {      return key   }})console.log(_quote_filter) //=> [ 'btc', undefined, undefined ]

Example 4: lodash map

_.each(markets, (obj, key) => {   obj.symbol = key})console.log(markets)//=> {       'BTC/USD': { buys: 0, sells: 3, symbol: 'BTC/USD' },       'DASH/BTC': { buys: 3, sells: 1, symbol: 'DASH/BTC' },       'ETH/BTC': { buys: 3, sells: 2, symbol: 'ETH/BTC' }    }

Example 5: get lodash

var object = { 'a': [{ 'b': { 'c': 3 } }] }; _.get(object, 'a[0].b.c');// => 3 _.get(object, ['a', '0', 'b', 'c']);// => 3 _.get(object, 'a.b.c', 'default');// => 'default'

Example 6: node lodash documentation

$ npm i -g npm$ npm i --save lodash

Tags:

Misc Example