0 Tk

Object Methods

Manipulating Objects with Lodash

Lodash offers a variety of functions for working with objects. Here are some key object manipulation methods:

_.get(object, path, [defaultValue])

Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.

const 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'

_.merge(object, [sources])

Recursively merges own and inherited enumerable string keyed properties of source objects into the destination object.

const object = {
  'a': [{ 'b': 2 }, { 'd': 4 }]
};

const other = {
  'a': [{ 'c': 3 }, { 'e': 5 }]
};

_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }

_.omit(object, [paths])

Creates an object composed of the own and inherited enumerable property paths of object that are not omitted.

const object = { 'a': 1, 'b': '2', 'c': 3 };

_.omit(object, ['a', 'c']);
// => { 'b': '2' }

Previous: Array Methods Next: Function Methods

Last updated 1 day ago