- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.5.x
- Description
- Comprehensive JavaScript utility library with 300+ methods for arrays, objects, strings, functions, and more.
- Author
- tessl
- Last updated
object-methods.md docs/
1# Object Methods23Deep object manipulation including property access, merging, transformation, and introspection. These methods provide powerful tools for working with complex nested objects and data structures.45## Capabilities67### Property Access89#### get10Gets the value at path of object.1112```javascript { .api }13/**14* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place15* @param object - The object to query16* @param path - The path of the property to get17* @param defaultValue - The value returned for undefined resolved values18* @returns Returns the resolved value19*/20function get(object, path, defaultValue);21```2223#### set24Sets the value at path of object.2526```javascript { .api }27/**28* Sets the value at path of object. If a portion of path doesn't exist, it's created29* @param object - The object to modify30* @param path - The path of the property to set31* @param value - The value to set32* @returns Returns object33*/34function set(object, path, value);3536/**37* Like set except that it accepts customizer which is invoked to produce the objects of path38* @param object - The object to modify39* @param path - The path of the property to set40* @param value - The value to set41* @param customizer - The function to customize assigned values42* @returns Returns object43*/44function setWith(object, path, value, customizer);45```4647#### has48Checks if path is a direct property of object.4950```javascript { .api }51/**52* Checks if path is a direct property of object53* @param object - The object to query54* @param path - The path to check55* @returns Returns true if path exists, else false56*/57function has(object, path);5859/**60* Checks if path is a direct or inherited property of object61* @param object - The object to query62* @param path - The path to check63* @returns Returns true if path exists, else false64*/65function hasIn(object, path);66```6768#### at69Creates an array of values corresponding to paths of object.7071```javascript { .api }72/**73* Creates an array of values corresponding to paths of object74* @param object - The object to iterate over75* @param paths - The property paths to pick76* @returns Returns the picked values77*/78function at(object, ...paths);79```8081### Property Extraction8283#### keys & values84Gets the own enumerable property names and values of object.8586```javascript { .api }87/**88* Creates an array of the own enumerable property names of object89* @param object - The object to query90* @returns Returns the array of property names91*/92function keys(object);9394/**95* Creates an array of the own and inherited enumerable property names of object96* @param object - The object to query97* @returns Returns the array of property names98*/99function keysIn(object);100101/**102* Creates an array of the own enumerable string keyed property values of object103* @param object - The object to query104* @returns Returns the array of property values105*/106function values(object);107108/**109* Creates an array of the own and inherited enumerable string keyed property values of object110* @param object - The object to query111* @returns Returns the array of property values112*/113function valuesIn(object);114```115116#### toPairs117Creates an array of own enumerable string keyed-value pairs for object.118119```javascript { .api }120/**121* Creates an array of own enumerable string keyed-value pairs for object122* @param object - The object to query123* @returns Returns the key-value pairs124*/125function toPairs(object);126127/**128* Creates an array of own and inherited enumerable string keyed-value pairs for object129* @param object - The object to query130* @returns Returns the key-value pairs131*/132function toPairsIn(object);133```134135### Property Selection136137#### pick138Creates an object composed of the picked object properties.139140```javascript { .api }141/**142* Creates an object composed of the picked object properties143* @param object - The source object144* @param paths - The property paths to pick145* @returns Returns the new object146*/147function pick(object, ...paths);148149/**150* Creates an object composed of the object properties predicate returns truthy for151* @param object - The source object152* @param predicate - The function invoked per property153* @returns Returns the new object154*/155function pickBy(object, predicate);156```157158#### omit159Creates an object composed of the own and inherited enumerable property paths that are not omitted.160161```javascript { .api }162/**163* Creates an object composed of the own and inherited enumerable property paths that are not omitted164* @param object - The source object165* @param paths - The property paths to omit166* @returns Returns the new object167*/168function omit(object, ...paths);169170/**171* The opposite of pickBy; creates an object composed of the own and inherited enumerable string keyed properties of object that predicate doesn't return truthy for172* @param object - The source object173* @param predicate - The function invoked per property174* @returns Returns the new object175*/176function omitBy(object, predicate);177```178179### Object Merging180181#### assign182Assigns own enumerable string keyed properties of source objects to the destination object.183184```javascript { .api }185/**186* Assigns own enumerable string keyed properties of source objects to the destination object187* @param object - The destination object188* @param sources - The source objects189* @returns Returns object190*/191function assign(object, ...sources);192193/**194* Like assign except that it iterates over own and inherited source properties195* @param object - The destination object196* @param sources - The source objects197* @returns Returns object198*/199function assignIn(object, ...sources);200201/**202* Like assign but accepts customizer which is invoked to produce the assigned values203* @param object - The destination object204* @param sources - The source objects205* @param customizer - The function to customize assigned values206* @returns Returns object207*/208function assignWith(object, sources, customizer);209```210211#### merge212Recursively merges own and inherited enumerable string keyed properties of source objects into the destination object.213214```javascript { .api }215/**216* Recursively merges own and inherited enumerable string keyed properties of source objects into the destination object217* @param object - The destination object218* @param sources - The source objects219* @returns Returns object220*/221function merge(object, ...sources);222223/**224* Like merge but accepts customizer which is invoked to produce the merged values225* @param object - The destination object226* @param sources - The source objects227* @param customizer - The function to customize assigned values228* @returns Returns object229*/230function mergeWith(object, sources, customizer);231```232233### Object Defaults234235#### defaults236Assigns properties of source objects to the destination object for all destination properties that resolve to undefined.237238```javascript { .api }239/**240* Assigns properties of source objects to the destination object for all destination properties that resolve to undefined241* @param object - The destination object242* @param sources - The source objects243* @returns Returns object244*/245function defaults(object, ...sources);246247/**248* Like defaults except that it recursively assigns default properties249* @param object - The destination object250* @param sources - The source objects251* @returns Returns object252*/253function defaultsDeep(object, ...sources);254```255256### Object Transformation257258#### mapKeys & mapValues259Creates an object with the same values/keys as object and keys/values generated by running each property through iteratee.260261```javascript { .api }262/**263* Creates an object with the same values as object and keys generated by running each own enumerable string keyed property of object through iteratee264* @param object - The object to iterate over265* @param iteratee - The function invoked per iteration266* @returns Returns the new mapped object267*/268function mapKeys(object, iteratee);269270/**271* Creates an object with the same keys as object and values generated by running each own enumerable string keyed property of object through iteratee272* @param object - The object to iterate over273* @param iteratee - The function invoked per iteration274* @returns Returns the new mapped object275*/276function mapValues(object, iteratee);277```278279#### transform280An alternative to reduce; this method transforms object to a new accumulator object.281282```javascript { .api }283/**284* An alternative to reduce; this method transforms object to a new accumulator object which is the result of running each of its own enumerable string keyed properties through iteratee285* @param object - The object to iterate over286* @param iteratee - The function invoked per iteration287* @param accumulator - The custom accumulator value288* @returns Returns the accumulated value289*/290function transform(object, iteratee, accumulator);291```292293### Object Inversion294295#### invert296Creates an object composed of the inverted keys and values of object.297298```javascript { .api }299/**300* Creates an object composed of the inverted keys and values of object301* @param object - The object to invert302* @returns Returns the new inverted object303*/304function invert(object);305306/**307* Like invert except that the inverted object is generated from the results of running each element of object through iteratee308* @param object - The object to invert309* @param iteratee - The iteratee invoked per element310* @returns Returns the new inverted object311*/312function invertBy(object, iteratee);313```314315### Object Searching316317#### findKey318Like find except that it returns the key of the first element predicate returns truthy for instead of the element itself.319320```javascript { .api }321/**322* Like find except that it returns the key of the first element predicate returns truthy for instead of the element itself323* @param object - The object to inspect324* @param predicate - The function invoked per iteration325* @returns Returns the key of the matched element, else undefined326*/327function findKey(object, predicate);328329/**330* Like findKey except that it iterates over elements of a collection in the opposite order331* @param object - The object to inspect332* @param predicate - The function invoked per iteration333* @returns Returns the key of the matched element, else undefined334*/335function findLastKey(object, predicate);336```337338### Object Iteration339340#### forIn & forOwn341Iterates over own and inherited / own enumerable string keyed properties of an object.342343```javascript { .api }344/**345* Iterates over own and inherited enumerable string keyed properties of an object and invokes iteratee for each property346* @param object - The object to iterate over347* @param iteratee - The function invoked per iteration348* @returns Returns object349*/350function forIn(object, iteratee);351352/**353* Like forIn except that it iterates over properties in the opposite order354* @param object - The object to iterate over355* @param iteratee - The function invoked per iteration356* @returns Returns object357*/358function forInRight(object, iteratee);359360/**361* Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property362* @param object - The object to iterate over363* @param iteratee - The function invoked per iteration364* @returns Returns object365*/366function forOwn(object, iteratee);367368/**369* Like forOwn except that it iterates over properties in the opposite order370* @param object - The object to iterate over371* @param iteratee - The function invoked per iteration372* @returns Returns object373*/374function forOwnRight(object, iteratee);375```376377### Function Properties378379#### functions380Creates an array of function property names from own enumerable properties of object.381382```javascript { .api }383/**384* Creates an array of function property names from own enumerable properties of object385* @param object - The object to inspect386* @returns Returns the function names387*/388function functions(object);389390/**391* Creates an array of function property names from own and inherited enumerable properties of object392* @param object - The object to inspect393* @returns Returns the function names394*/395function functionsIn(object);396```397398### Method Invocation399400#### invoke401Invokes the method at path of object.402403```javascript { .api }404/**405* Invokes the method at path of object406* @param object - The object to query407* @param path - The path of the method to invoke408* @param args - The arguments to invoke the method with409* @returns Returns the result of the invoked method410*/411function invoke(object, path, ...args);412```413414### Property Updates415416#### update417Like set except that it accepts updater to produce the value to set.418419```javascript { .api }420/**421* Like set except that it accepts updater to produce the value to set422* @param object - The object to modify423* @param path - The path of the property to set424* @param updater - The function to produce the updated value425* @returns Returns object426*/427function update(object, path, updater);428429/**430* Like update but accepts customizer which is invoked to produce the objects of path431* @param object - The object to modify432* @param path - The path of the property to set433* @param updater - The function to produce the updated value434* @param customizer - The function to customize assigned values435* @returns Returns object436*/437function updateWith(object, path, updater, customizer);438```439440#### unset441Removes the property at path of object.442443```javascript { .api }444/**445* Removes the property at path of object446* @param object - The object to modify447* @param path - The path of the property to unset448* @returns Returns true if the property is deleted, else false449*/450function unset(object, path);451```452453### Object Creation454455#### create456Creates an object that inherits from the prototype object.457458```javascript { .api }459/**460* Creates an object that inherits from the prototype object461* @param prototype - The object to inherit from462* @param properties - The properties to assign to the object463* @returns Returns the new object464*/465function create(prototype, properties);466```467468### Value Resolution469470#### result471Like get except that if the resolved value is a function it's invoked with the this binding of its parent object and its result is returned.472473```javascript { .api }474/**475* Like get except that if the resolved value is a function it's invoked with the this binding of its parent object and its result is returned476* @param object - The object to query477* @param path - The path of the property to resolve478* @param defaultValue - The value returned for undefined resolved values479* @returns Returns the resolved value480*/481function result(object, path, defaultValue);482```483484## Property Path Formats485486Lodash supports multiple formats for property paths:487488```javascript { .api }489// String paths490get(object, 'a.b.c'); // → object.a.b.c491get(object, 'a[0].b.c'); // → object.a[0].b.c492493// Array paths494get(object, ['a', 'b', 'c']); // → object.a.b.c495get(object, ['a', 0, 'b']); // → object.a[0].b496497// Mixed paths498get(object, 'users[0].name'); // → object.users[0].name499```500501### Advanced Assignment Methods502503#### assignInWith504Like assignIn but accepts customizer for specifying assigned values.505506```javascript { .api }507/**508* Like assignIn but accepts customizer for specifying assigned values509* @param object - The destination object510* @param sources - The source objects511* @param customizer - The function to customize assigned values512* @returns Returns object513*/514function assignInWith(object, ...sources, customizer);515```516517### Method Aliases518519#### extend & extendWith520Aliases for assignIn and assignInWith respectively.521522```javascript { .api }523// Aliases524const extend = assignIn;525const extendWith = assignInWith;526```527528#### first529Alias for head function from array methods.530531```javascript { .api }532/**533* Gets the first element of array (alias for head)534* @param array - The array to query535* @returns Returns the first element of array536*/537const first = head;538```539540## Usage Examples541542```javascript543import {544get, set, has, pick, omit, merge, assign,545keys, values, mapValues, transform546} from "lodash";547548const user = {549id: 1,550name: 'John Doe',551profile: {552age: 30,553address: {554city: 'New York',555country: 'USA'556}557},558roles: ['user', 'admin'],559settings: {560theme: 'dark',561notifications: true562}563};564565// Property access566const name = get(user, 'name'); // 'John Doe'567const city = get(user, 'profile.address.city'); // 'New York'568const role = get(user, 'roles[0]'); // 'user'569const missing = get(user, 'profile.bio', 'N/A'); // 'N/A'570571// Property modification572const updated = set({...user}, 'profile.age', 31);573const withEmail = set({...user}, 'profile.email', 'john@example.com');574575// Property checking576const hasName = has(user, 'name'); // true577const hasEmail = has(user, 'profile.email'); // false578579// Property selection580const basic = pick(user, ['id', 'name']);581// Result: { id: 1, name: 'John Doe' }582583const publicProfile = omit(user, ['settings', 'roles']);584// Result: user object without settings and roles585586// Object merging587const defaults = { theme: 'light', language: 'en' };588const userSettings = merge({}, defaults, user.settings);589// Result: { theme: 'dark', language: 'en', notifications: true }590591// Property extraction592const propertyNames = keys(user);593// Result: ['id', 'name', 'profile', 'roles', 'settings']594595const propertyValues = values(user.settings);596// Result: ['dark', true]597598// Object transformation599const normalized = mapValues(user.profile.address, val =>600typeof val === 'string' ? val.toUpperCase() : val601);602// Result: { city: 'NEW YORK', country: 'USA' }603604// Complex transformation605const flattened = transform(user, (result, value, key) => {606if (typeof value === 'object' && !Array.isArray(value)) {607Object.assign(result, mapKeys(value, (v, k) => `${key}.${k}`));608} else {609result[key] = value;610}611}, {});612613// Working with nested data614const posts = [615{ id: 1, author: { name: 'John' }, tags: ['js', 'web'] },616{ id: 2, author: { name: 'Jane' }, tags: ['react'] }617];618619const authorNames = posts.map(post => get(post, 'author.name'));620// Result: ['John', 'Jane']621622// Safe property updates623const safeUpdate = (obj, path, value) => {624const copy = { ...obj };625return set(copy, path, value);626};627628const updatedUser = safeUpdate(user, 'profile.lastLogin', new Date());629```