- Spec files
npm-lodash
Describes: pkg:npm/lodash@4.6.x
- Description
- A modern JavaScript utility library delivering modularity, performance & extras
- Author
- tessl
- Last updated
object.md docs/
1# Object Utilities23Object manipulation including property access, assignment, merging, and transformation utilities. These methods provide safe and flexible ways to work with JavaScript objects.45## Capabilities67### Property Assignment89Assign properties from source objects to target objects.1011```javascript { .api }12/**13* Assigns own enumerable string keyed properties of source objects to the14* destination object. Source objects are applied from left to right.15*16* @param {Object} object The destination object17* @param {...Object} [sources] The source objects18* @returns {Object} Returns `object`19*/20function assign(object: Object, ...sources: Object[]): Object;2122/**23* This method is like `assign` except that it iterates over own and24* inherited source properties.25*26* @param {Object} object The destination object27* @param {...Object} [sources] The source objects28* @returns {Object} Returns `object`29*/30function assignIn(object: Object, ...sources: Object[]): Object;3132/**33* This method is like `assign` except that it accepts `customizer`34* which is invoked to produce the assigned values.35*36* @param {Object} object The destination object37* @param {...Object} [sources] The source objects38* @param {Function} [customizer] The function to customize assigned values39* @returns {Object} Returns `object`40*/41function assignWith(object: Object, ...sources: Object[], customizer: Function): Object;4243/**44* This method is like `assignIn` except that it accepts `customizer`45* which is invoked to produce the assigned values.46*47* @param {Object} object The destination object48* @param {...Object} [sources] The source objects49* @param {Function} [customizer] The function to customize assigned values50* @returns {Object} Returns `object`51*/52function assignInWith(object: Object, ...sources: Object[], customizer: Function): Object;53```5455### Property Access5657Get and set object properties safely.5859```javascript { .api }60/**61* Gets the value at `path` of `object`. If the resolved value is62* `undefined`, the `defaultValue` is returned in its place.63*64* @param {Object} object The object to query65* @param {Array|string} path The path of the property to get66* @param {*} [defaultValue] The value returned for `undefined` resolved values67* @returns {*} Returns the resolved value68*/69function get(object: Object, path: Array|string, defaultValue?: any): any;7071/**72* Sets the value at `path` of `object`. If a portion of `path` doesn't exist,73* it's created. Arrays are created for missing index properties while objects74* are created for all other missing properties.75*76* @param {Object} object The object to modify77* @param {Array|string} path The path of the property to set78* @param {*} value The value to set79* @returns {Object} Returns `object`80*/81function set(object: Object, path: Array|string, value: any): Object;8283/**84* This method is like `set` except that it accepts `customizer` which is85* invoked to produce the objects of `path`.86*87* @param {Object} object The object to modify88* @param {Array|string} path The path of the property to set89* @param {*} value The value to set90* @param {Function} [customizer] The function to customize assigned values91* @returns {Object} Returns `object`92*/93function setWith(object: Object, path: Array|string, value: any, customizer?: Function): Object;9495/**96* Removes the property at `path` of `object`.97*98* @param {Object} object The object to modify99* @param {Array|string} path The path of the property to unset100* @returns {boolean} Returns `true` if the property is deleted, else `false`101*/102function unset(object: Object, path: Array|string): boolean;103```104105**Usage Examples:**106107```javascript108import { get, set, unset } from "lodash";109110const obj = { a: { b: { c: 3 } } };111112// Safe property access113get(obj, 'a.b.c'); // 3114get(obj, 'a.b.d', 'default'); // 'default'115get(obj, ['a', 'b', 'c']); // 3116117// Property setting118set(obj, 'a.b.d', 4);119// obj is now { a: { b: { c: 3, d: 4 } } }120121// Property removal122unset(obj, 'a.b.c');123// obj is now { a: { b: { d: 4 } } }124```125126### Property Checking127128Check for property existence.129130```javascript { .api }131/**132* Checks if `path` is a direct property of `object`.133*134* @param {Object} object The object to query135* @param {Array|string} path The path to check136* @returns {boolean} Returns `true` if `path` exists, else `false`137*/138function has(object: Object, path: Array|string): boolean;139140/**141* Checks if `path` is a direct or inherited property of `object`.142*143* @param {Object} object The object to query144* @param {Array|string} path The path to check145* @returns {boolean} Returns `true` if `path` exists, else `false`146*/147function hasIn(object: Object, path: Array|string): boolean;148```149150### Property Selection151152Select or omit properties from objects.153154```javascript { .api }155/**156* Creates an object composed of the picked `object` properties.157*158* @param {Object} object The source object159* @param {...(string|string[])} [paths] The property paths to pick160* @returns {Object} Returns the new object161*/162function pick(object: Object, ...paths: (string|string[])[]): Object;163164/**165* Creates an object composed of the `object` properties `predicate` returns166* truthy for.167*168* @param {Object} object The source object169* @param {Function|Object|string} [predicate] The function invoked per property170* @returns {Object} Returns the new object171*/172function pickBy(object: Object, predicate?: Function|Object|string): Object;173174/**175* The opposite of `pick`; this method creates an object composed of the176* own and inherited enumerable property paths of `object` that are not omitted.177*178* @param {Object} object The source object179* @param {...(string|string[])} [paths] The property paths to omit180* @returns {Object} Returns the new object181*/182function omit(object: Object, ...paths: (string|string[])[]): Object;183184/**185* The opposite of `pickBy`; this method creates an object composed of186* the own and inherited enumerable string keyed properties of `object` that187* `predicate` doesn't return truthy for.188*189* @param {Object} object The source object190* @param {Function|Object|string} [predicate] The function invoked per property191* @returns {Object} Returns the new object192*/193function omitBy(object: Object, predicate?: Function|Object|string): Object;194```195196**Usage Examples:**197198```javascript199import { pick, pickBy, omit, omitBy } from "lodash";200201const user = { name: 'Alice', age: 25, email: 'alice@example.com', password: 'secret' };202203// Pick specific properties204pick(user, ['name', 'email']);205// Result: { name: 'Alice', email: 'alice@example.com' }206207// Pick by predicate208pickBy(user, value => typeof value === 'string');209// Result: { name: 'Alice', email: 'alice@example.com', password: 'secret' }210211// Omit specific properties212omit(user, ['password']);213// Result: { name: 'Alice', age: 25, email: 'alice@example.com' }214215// Omit by predicate216omitBy(user, value => typeof value === 'string');217// Result: { age: 25 }218```219220### Object Merging221222Merge objects deeply or shallowly.223224```javascript { .api }225/**226* This method is like `assign` except that it recursively merges own and227* inherited enumerable string keyed properties of source objects into the228* destination object.229*230* @param {Object} object The destination object231* @param {...Object} [sources] The source objects232* @returns {Object} Returns `object`233*/234function merge(object: Object, ...sources: Object[]): Object;235236/**237* This method is like `merge` except that it accepts `customizer` which238* is invoked to produce the merged values of the destination and source properties.239*240* @param {Object} object The destination object241* @param {...Object} [sources] The source objects242* @param {Function} [customizer] The function to customize assigned values243* @returns {Object} Returns `object`244*/245function mergeWith(object: Object, ...sources: Object[], customizer: Function): Object;246```247248### Default Values249250Assign default properties to objects.251252```javascript { .api }253/**254* Assigns own and inherited enumerable string keyed properties of source255* objects to the destination object for all destination properties that256* resolve to `undefined`.257*258* @param {Object} object The destination object259* @param {...Object} [sources] The source objects260* @returns {Object} Returns `object`261*/262function defaults(object: Object, ...sources: Object[]): Object;263264/**265* This method is like `defaults` except that it recursively assigns266* default properties.267*268* @param {Object} object The destination object269* @param {...Object} [sources] The source objects270* @returns {Object} Returns `object`271*/272function defaultsDeep(object: Object, ...sources: Object[]): Object;273```274275### Object Keys and Values276277Extract keys, values, and pairs from objects.278279```javascript { .api }280/**281* Creates an array of the own enumerable property names of `object`.282*283* @param {Object} object The object to query284* @returns {Array} Returns the array of property names285*/286function keys(object: Object): Array;287288/**289* Creates an array of the own and inherited enumerable property names of `object`.290*291* @param {Object} object The object to query292* @returns {Array} Returns the array of property names293*/294function keysIn(object: Object): Array;295296/**297* Creates an array of the own enumerable string keyed property values of `object`.298*299* @param {Object} object The object to query300* @returns {Array} Returns the array of property values301*/302function values(object: Object): Array;303304/**305* Creates an array of the own and inherited enumerable string keyed property306* values of `object`.307*308* @param {Object} object The object to query309* @returns {Array} Returns the array of property values310*/311function valuesIn(object: Object): Array;312313/**314* Creates an array of own enumerable string keyed-value pairs for `object`.315*316* @param {Object} object The object to query317* @returns {Array} Returns the key-value pairs318*/319function toPairs(object: Object): Array;320321/**322* Creates an array of own and inherited enumerable string keyed-value pairs323* for `object`.324*325* @param {Object} object The object to query326* @returns {Array} Returns the key-value pairs327*/328function toPairsIn(object: Object): Array;329```330331### Object Creation332333Create new objects with specific prototypes.334335```javascript { .api }336/**337* Creates an object that inherits from the `prototype` object. If a338* `properties` object is given, its own enumerable string keyed properties339* are assigned to the created object.340*341* @param {Object} prototype The object to inherit from342* @param {Object} [properties] The properties to assign to the object343* @returns {Object} Returns the new object344*/345function create(prototype: Object, properties?: Object): Object;346```347348### Object Inversion349350Invert object key-value relationships.351352```javascript { .api }353/**354* Creates an object composed of the inverted keys and values of `object`.355* If `object` contains duplicate values, subsequent values overwrite356* property assignments of previous values.357*358* @param {Object} object The object to invert359* @returns {Object} Returns the new inverted object360*/361function invert(object: Object): Object;362363/**364* This method is like `invert` except that the inverted object is generated365* from the results of running each element of `object` thru `iteratee`.366*367* @param {Object} object The object to invert368* @param {Function|Object|string} [iteratee] The iteratee invoked per element369* @returns {Object} Returns the new inverted object370*/371function invertBy(object: Object, iteratee?: Function|Object|string): Object;372```373374### Function Properties375376Get function property names from objects.377378```javascript { .api }379/**380* Creates an array of function property names from own enumerable properties381* of `object`.382*383* @param {Object} object The object to inspect384* @returns {Array} Returns the function names385*/386function functions(object: Object): Array;387388/**389* Creates an array of function property names from own and inherited390* enumerable properties of `object`.391*392* @param {Object} object The object to inspect393* @returns {Array} Returns the function names394*/395function functionsIn(object: Object): Array;396```397398### Object Iteration399400Iterate over object properties.401402```javascript { .api }403/**404* Iterates over own enumerable string keyed properties of an object and405* invokes `iteratee` for each property.406*407* @param {Object} object The object to iterate over408* @param {Function} [iteratee] The function invoked per iteration409* @returns {Object} Returns `object`410*/411function forOwn(object: Object, iteratee?: Function): Object;412413/**414* This method is like `forOwn` except that it iterates over properties in reverse order.415*416* @param {Object} object The object to iterate over417* @param {Function} [iteratee] The function invoked per iteration418* @returns {Object} Returns `object`419*/420function forOwnRight(object: Object, iteratee?: Function): Object;421422/**423* Iterates over own and inherited enumerable string keyed properties of an424* object and invokes `iteratee` for each property.425*426* @param {Object} object The object to iterate over427* @param {Function} [iteratee] The function invoked per iteration428* @returns {Object} Returns `object`429*/430function forIn(object: Object, iteratee?: Function): Object;431432/**433* This method is like `forIn` except that it iterates over properties in reverse order.434*435* @param {Object} object The object to iterate over436* @param {Function} [iteratee] The function invoked per iteration437* @returns {Object} Returns `object`438*/439function forInRight(object: Object, iteratee?: Function): Object;440```441442### Object Search443444Find properties in objects.445446```javascript { .api }447/**448* This method is like `find` except that it returns the key of the first449* element `predicate` returns truthy for instead of the element itself.450*451* @param {Object} object The object to inspect452* @param {Function|Object|string} [predicate] The function invoked per iteration453* @returns {string|undefined} Returns the key of the matched element, else `undefined`454*/455function findKey(object: Object, predicate?: Function|Object|string): string|undefined;456457/**458* This method is like `findKey` except that it iterates over elements in reverse order.459*460* @param {Object} object The object to inspect461* @param {Function|Object|string} [predicate] The function invoked per iteration462* @returns {string|undefined} Returns the key of the matched element, else `undefined`463*/464function findLastKey(object: Object, predicate?: Function|Object|string): string|undefined;465```466467### Object Transformation468469Transform objects with custom logic.470471```javascript { .api }472/**473* An alternative to `reduce`; this method transforms `object` to a new474* `accumulator` object which is the result of running each of its own475* enumerable string keyed properties thru `iteratee`.476*477* @param {Object} object The object to iterate over478* @param {Function} [iteratee] The function invoked per iteration479* @param {*} [accumulator] The custom accumulator value480* @returns {*} Returns the accumulated value481*/482function transform(object: Object, iteratee?: Function, accumulator?: any): any;483484/**485* The opposite of `mapValues`; this method creates an object with the same values486* as `object` and keys generated by running each own enumerable string keyed487* property of `object` thru `iteratee`.488*489* @param {Object} object The object to iterate over490* @param {Function|Object|string} [iteratee] The function invoked per iteration491* @returns {Object} Returns the new mapped object492*/493function mapKeys(object: Object, iteratee?: Function|Object|string): Object;494495/**496* Creates an object with the same keys as `object` and values generated by497* running each own enumerable string keyed property of `object` thru `iteratee`.498*499* @param {Object} object The object to iterate over500* @param {Function|Object|string} [iteratee] The function invoked per iteration501* @returns {Object} Returns the new mapped object502*/503function mapValues(object: Object, iteratee?: Function|Object|string): Object;504505/**506* This method is like `get` except that if the resolved value is a function507* it's invoked with the `this` binding of its parent object and its result is returned.508*509* @param {Object} object The object to query510* @param {Array|string} path The path of the property to resolve511* @param {*} [defaultValue] The value returned for `undefined` resolved values512* @returns {*} Returns the resolved value513*/514function result(object: Object, path: Array|string, defaultValue?: any): any;515```516517### Method Invocation518519Invoke methods on objects.520521```javascript { .api }522/**523* Invokes the method at `path` of `object`.524*525* @param {Object} object The object to query526* @param {Array|string} path The path of the method to invoke527* @param {...*} [args] The arguments to invoke the method with528* @returns {*} Returns the result of the invoked method529*/530function invoke(object: Object, path: Array|string, ...args: any[]): any;531```532533### Property Updates534535Update object properties with functions.536537```javascript { .api }538/**539* This method is like `set` except that accepts `updater` to produce the540* value to set.541*542* @param {Object} object The object to modify543* @param {Array|string} path The path of the property to set544* @param {Function} updater The function to produce the updated value545* @returns {Object} Returns `object`546*/547function update(object: Object, path: Array|string, updater: Function): Object;548549/**550* This method is like `update` except that it accepts `customizer` which is551* invoked to produce the objects of `path`.552*553* @param {Object} object The object to modify554* @param {Array|string} path The path of the property to set555* @param {Function} updater The function to produce the updated value556* @param {Function} [customizer] The function to customize assigned values557* @returns {Object} Returns `object`558*/559function updateWith(object: Object, path: Array|string, updater: Function, customizer?: Function): Object;560```