lodash filter array code example

Example 1: lodash deep clone object

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

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

Example 2: lodash filter object keys

var user = {
  "first_name":"taylor",
  "last_name":"hawkes",
  "full_name": "taylor hawkes"
};

var userCleaned = _.pickBy(user, function(value, key) {
  return (key === "first_name" || key === "last_name");
});

//userCleaned is now:
//{
//   "first_name": "taylor",
//   "last_name": "hawkes"
//}

Example 3: Lodash filter

var users = [
  { 'user': 'barney', 'age': 36, 'active': true },
  { 'user': 'fred',   'age': 40, 'active': false }
];
 
_.filter(users, function(o) { return !o.active; });
// => objects for ['fred']
 
// The `_.matches` iteratee shorthand.
_.filter(users, { 'age': 36, 'active': true });
// => objects for ['barney']
 
// The `_.matchesProperty` iteratee shorthand.
_.filter(users, ['active', false]);
// => objects for ['fred']
 
// The `_.property` iteratee shorthand.
_.filter(users, 'active');
// => objects for ['barney']

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'

Tags:

Misc Example