lodash size code example

Example 1: import lodash

// import entire library
import _ from "lodash"
const nums = [1, 2, 2, 3, 1, 4]
let res = _.uniq(nums)

// import methods by name
// Still loads entire lodash library!!
import { uniq } from "lodash"
const nums = [1, 2, 2, 3, 1, 4]
let res = uniq(nums) // better readability

// import only what you need
import uniq from "loadash/uniq"
const nums = [1, 2, 2, 3, 1, 4]
let res = uniq(nums)

Example 2: how to loop through an object using lodash

_.forOwn(obj, function(value, key) { } );

Example 3: lodash deep clone object

const clone = require('lodash/clone'); 
const cloneDeep = require('lodash/clonedeep');

const shallowCopy = clone(originalObject);
const deepCopy = clonedeep(originalObject);

Example 4: 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