Object methods
Explore the list of utility methods for working with objects.
This documentation covers the methods provided by the objectUtils
library, along with examples for each method.
List of objectUtils methods
objectUtils.assign(target, ...sources)
Assigns enumerable properties from one or more source objects to a target object.
Parameters
target
(Object): The target object....sources
(Object): The source objects.
Returns
- (Object): The modified target object.
Example
const target = { a: 1 };
const source = { b: 2 };
const result = objectUtils.assign(target, source);
// Result: { a: 1, b: 2 }
objectUtils.assignIn(target, ...sources)
This method is like objectUtils.assign except that it assigns source properties to the target object.
Parameters
target
(Object): The target object....sources
(Object): The source objects.
Returns
- (Object): The modified target object.
Example
const target = { a: 1 };
const source = { b: 2 };
const result = objectUtils.assignIn(target, source);
// Result: { a: 1, b: 2 }
objectUtils.assignInWith(target, ...sources, customizer)
This method is like objectUtils.assignIn except that it accepts a customizer which is invoked to produce the assigned values.
Parameters
target
(Object): The target object....sources
(Object): The source objects.customizer
(Function): The customizer function.
Returns
- (Object): The modified target object.
Example
const target = { a: 1 };
const source = { b: 2 };
const customizer = (objValue, srcValue) => objValue === undefined ? srcValue : objValue;
const result = objectUtils.assignInWith(target, source, customizer);
// Result: { a: 1, b: 2 }
objectUtils.assignWith(target, ...sources, customizer)
This method is like objectUtils.assign except that it accepts a customizer which is invoked to produce the assigned values.
Parameters
target
(Object): The target object....sources
(Object): The source objects.customizer
(Function): The customizer function.
Returns
- (Object): The modified target object.
Example
const target = { a: 1 };
const source = { b: 2 };
const customizer = (objValue, srcValue) => objValue === undefined ? srcValue : objValue;
const result = objectUtils.assignWith(target, source, customizer);
// Result: { a: 1, b: 2 }
objectUtils.at(object, paths)
Creates an array of values corresponding to paths of an object.
Parameters
object
(Object): The object to iterate over.paths
(Array|string): The property paths to get.
Returns
- (Array): The array of values.
Example
const object = { a: [{ b: { c: 3 } }, 4] };
const result = objectUtils.at(object, ['a[0].b.c', 'a[1]']);
// Result: [3, 4]
objectUtils.create(prototype, properties)
Creates an object that inherits from the given prototype object. Optionally, properties can be assigned to the created object.
Parameters
prototype
(Object): The prototype object.properties
(Object): The properties to assign.
Returns
- (Object): The created object.
Example
const person = { name: 'John' };
const student = objectUtils.create(person, { studentId: '123' });
// Result: { name: 'John', studentId: '123' }
objectUtils.defaults(object, ...defaults)
Assigns default properties to an object if they are not already defined.
Parameters
object
(Object): The target object....defaults
(Object): The default objects.
Returns
- (Object): The modified target object.
Example
const object = { a: 1 };
const defaults = { b: 2 };
const result = objectUtils.defaults(object, defaults);
// Result: { a: 1, b: 2 }
objectUtils.defaultsDeep(object, ...defaults)
This method is like objectUtils.defaults except that it recursively assigns default properties.
Parameters
object
(Object): The target object....defaults
(Object): The default objects.
Returns
- (Object): The modified target object.
Example
const object = { a: { b: 2 } };
const defaults = { a: { b: 1, c: 3 } };
const result = objectUtils.defaultsDeep(object, defaults);
// Result: { a: { b: 2, c: 3 } }
objectUtils.entries(object)
Creates an array of key-value pairs from own enumerable properties of an object.
Parameters
object
(Object): The object to convert.
Returns
- (Array): The array of key-value pairs.
Example
const object = { a: 1, b: 2 };
const result = objectUtils.entries(object);
// Result: [['a', 1], ['b', 2]]
objectUtils.entriesIn(object)
Creates an array of key-value pairs from all enumerable properties of an object.
Parameters
object
(Object): The object to convert.
Returns
- (Array): The array of key-value pairs.
Example
const object = Object.create({ a: 1 });
object.b = 2;
const result = objectUtils.entriesIn(object);
// Result: [['a', 1], ['b', 2]]
objectUtils.extend(destination, ...sources)
Extends the properties of the destination object by copying properties from one or more sources objects.
Parameters
destination
(Object): The target object....sources
(Object): The source objects.
Returns
- (Object): The modified destination object.
objectUtils.extendWith(destination, ...sources, customizer)
This method is like objectUtils.extend except that it accepts a customizer which is invoked to produce the assigned values.
Parameters
destination
(Object): The target object....sources
(Object): The source objects.customizer
(Function): The customizer function.
Returns
- (Object): The modified destination object.
Example
const destination = { a: 1 };
const source = { b: 2 };
const customizer = (objValue, srcValue) => objValue === undefined ? srcValue : objValue;
const result = objectUtils.extendWith(destination, source, customizer);
// Result: { a: 1, b: 2 }
objectUtils.findKey(object, predicate)
This method is like arrayUtils.find for objects. It returns the key of the first element for which the predicate returns truthy.
Parameters
object
(Object): The object to search.predicate
(Function): The predicate function.
Returns
- (string|undefined): The key of the matched element, or undefined if not found.
Example
const object = { a: 1, b: 2, c: 3 };
const result = objectUtils.findKey(object, item => item % 2 === 0);
// Result: 'b'
objectUtils.findLastKey(object, predicate)
This method is like objectUtils.findKey except that it iterates over properties in reverse.
Parameters
object
(Object): The object to search.predicate
(Function): The predicate function.
Returns
- (string|undefined): The key of the matched element, or undefined if not found.
Example
const object = { a: 1, b: 2, c: 3 };
const result = objectUtils.findLastKey(object, item => item % 2 === 0);
// Result: 'b'
objectUtils.forIn(object, iteratee)
Iterates over own enumerable properties of an object, invoking the iteratee function for each property.
Parameters
object
(Object): The object to iterate over.iteratee
(Function): The iteratee function.
Returns
- (Object): The original object.
Example
const object = { a: 1, b: 2 };
objectUtils.forIn(object, (value, key) => console.log(key, value));
// Result: Logs 'a 1' and 'b 2'
objectUtils.forInRight(object, iteratee)
This method is like objectUtils.forIn except that it iterates over properties in reverse.
Parameters
object
(Object): The object to iterate over.iteratee
(Function): The iteratee function.
Returns
- (Object): The original object.
Example
const object = { a: 1, b: 2 };
objectUtils.forInRight(object, (value, key) => console.log(key, value));
// Result: Logs 'b 2' and 'a 1'
objectUtils.forOwn(object, iteratee)
Iterates over own enumerable properties of an object, invoking the iteratee function for each property.
Parameters
object
(Object): The object to iterate over.iteratee
(Function): The iteratee function.
Returns
- (Object): The original object.
Example
const object = { a: 1, b: 2 };
objectUtils.forOwn(object, (value, key) => console.log(key, value));
// Result: Logs 'a 1' and 'b 2'
objectUtils.forOwnRight(object, iteratee)
This method is like objectUtils.forOwn except that it iterates over properties in reverse.
Parameters
object
(Object): The object to iterate over.iteratee
(Function): The iteratee function.
Returns
- (Object): The original object.
Example
const object = { a: 1, b: 2 };
objectUtils.forOwnRight(object, (value, key) => console.log(key, value));
// Result: Logs 'b 2' and 'a 1'
objectUtils.functions(object)
Creates an array of function property names from own enumerable properties of an object.
Parameters
object
(Object): The object to iterate over.
Returns
- (Array): The array of function property names.
Example
const object = { a: () => {}, b: 2, c: () => {} };
const result = objectUtils.functions(object);
// Result: ['a', 'c']
objectUtils.functionsIn(object)
Creates an array of function property names from all enumerable properties of an object.
Parameters
object
(Object): The object to iterate over.
Returns
- (Array): The array of function property names.
Example
const object = Object.create({ a: () => {} });
object.b = () => {};
const result = objectUtils.functionsIn(object);
// Result: ['a', 'b']
objectUtils.get(object, path, defaultValue)
Gets the value at path of an object.
Parameters
object
(Object): The object to query.path
(Array|string): The path of the property to get.defaultValue
(*): The value to return if the property is not found.
Returns
- (*): The value at the specified path.
Example
const object = { a: { b: 2 } };
const result = objectUtils.get(object, 'a.b');
// Result: 2
objectUtils.has(object, path)
Checks if a path is a direct property of an object.
Parameters
object
(Object): The object to query.path
(Array|string): The path to check.
Returns
- (boolean): true if the property exists, false otherwise.
Example
const object = { a: { b: 2 } };
const result = objectUtils.has(object, 'a.b');
// Result: true
objectUtils.hasIn(object, path)
Checks if a path is a property in an object.
Parameters
object
(Object): The object to query.path
(Array|string): The path to check.
Returns
- (boolean): true if the property exists, false otherwise.
Example
const object = { a: { b: 2 } };
const result = objectUtils.hasIn(object, 'a.b');
// Result: true
objectUtils.invert(object)
Creates an object composed of inverted keys and values of an object.
Parameters
object
(Object): The object to invert.
Returns
- (Object): The inverted object.
Example
const object = { a: 1, b: 2 };
const result = objectUtils.invert(object);
// Result: { '1': 'a', '2': 'b' }
objectUtils.invertBy(object, iteratee)
This method is like objectUtils.invert except that it accepts an iteratee which is invoked for each property.
Parameters
object
(Object): The object to invert.iteratee
(Function): The iteratee function.
Returns
- (Object): The inverted object.
Example
const object = { a: 1, b: 2, c: 3 };
const result = objectUtils.invertBy(object, value => value % 2 === 0 ? 'even' : 'odd');
// Result: { odd: ['a', 'c'], even: ['b'] }
objectUtils.invoke(object, path, ...args)
Invokes the method at path of an object.
Parameters
object
(Object): The object to query.path
(Array|string): The path of the method to invoke....args
(*): The arguments to invoke the method with.
Returns
- (*): The result of the invoked method.
Example
const object = { a: { b: (...args) => args.join(', ') } };
const result = objectUtils.invoke(object, 'a.b', 'x', 'y');
// Result: 'x, y'
objectUtils.keys(object)
Creates an array of the own enumerable property names of an object.
Parameters
object
(Object): The object to query.
Returns
- (Array): The array of property names.
Example
const object = { a: 1, b: 2 };
const result = objectUtils.keys(object);
// Result: ['a', 'b']
objectUtils.keysIn(object)
Creates an array of all enumerable property names of an object.
Parameters
object
(Object): The object to query.
Returns
- (Array): The array of property names.
Example
const object = Object.create({ a: 1 });
object.b = 2;
const result = objectUtils.keysIn(object);
// Result: ['a', 'b']
objectUtils.mapValues(object, iteratee)
Creates an object with the same keys as the given object and values generated by running each value of the object through the iteratee.
Parameters
object
(Object): The object to iterate over.iteratee
(Function): The iteratee function.
Returns
- (Object): The mapped object.
Example
const object = { a: 1, b: 2 };
const result = objectUtils.mapValues(object, value => value * 2);
// Result: { a: 2, b: 4 }
objectUtils.merge(object, ...sources)
Merges own and inherited enumerable properties of source objects into the target object.
Parameters
object
(Object): The target object....sources
(Object): The source objects.
Returns
- (Object): The modified target object.
Example
const target = { a: 1 };
const source = { b: 2 };
const result = objectUtils.merge(target, source);
// Result: { a: 1, b: 2 }
objectUtils.mergeWith(object, ...sources, customizer)
This method is like objectUtils.merge except that it accepts a customizer which is invoked to produce the assigned values.
Parameters
object
(Object): The target object....sources
(Object): The source objects.customizer
(Function): The customizer function.
Returns
- (Object): The modified target object.
Example
const target = { a: 1 };
const source = { b: 2 };
const customizer = (objValue, srcValue) => objValue === undefined ? srcValue : objValue;
const result = objectUtils.mergeWith(target, source, customizer);
// Result: { a: 1, b: 2 }
objectUtils.omit(object, paths)
Creates an object composed of the own enumerable property paths of an object that are not omitted.
Parameters
object
(Object): The object to iterate over.paths
(Array|string): The property paths to omit.
Returns
- (Object): The new object.
Example
const object = { a: 1, b: 2, c: 3 };
const result = objectUtils.omit(object, ['a', 'c']);
// Result: { b: 2 }
objectUtils.omitBy(object, predicate)
Creates an object composed of the own enumerable properties of an object that the predicate doesn't return truthy for.
Parameters
object
(Object): The object to iterate over.predicate
(Function): The predicate function.
Returns
- (Object): The new object.
Example
const object = { a: 1, b: 2, c: 3 };
const result = objectUtils.omitBy(object, value => value % 2 === 0);
// Result: { a: 1, c: 3 }
objectUtils.pick(object, paths)
Creates an object composed of the own enumerable property paths of an object that are picked.
Parameters
object
(Object): The object to iterate over.paths
(Array|string): The property paths to pick.
Returns
- (Object): The new object.
Example
const object = { a: 1, b: 2, c: 3 };
const result = objectUtils.pick(object, ['a', 'c']);
// Result: { a: 1, c: 3 }
objectUtils.pickBy(object, predicate)
Creates an object composed of the own enumerable properties of an object that the predicate returns truthy for.
Parameters
object
(Object): The object to iterate over.predicate
(Function): The predicate function.
Returns
- (Object): The new object.
Example
const object = { a: 1, b: 2, c: 3 };
const result = objectUtils.pickBy(object, value => value % 2 === 0);
// Result: { b: 2 }
objectUtils.result(object, path, defaultValue)
Resolves the value of property at path of an object. If the resolved value is a function, it is invoked with this binding of the object.
Parameters
object
(Object): The object to query.path
(Array|string): The path of the property to resolve.defaultValue
(*): The value to return if the property is not found.
Returns
- (*): The resolved value.
Example
const object = { a: { b: () => 2 } };
const result = objectUtils.result(object, 'a.b');
// Result: 2
objectUtils.set(object, path, value)
Sets the value at path of an object.
Parameters
object
(Object): The object to modify.path
(Array|string): The path of the property to set.value
(*): The value to set.
Returns
- (Object): The modified object.
Example
const object = { a: { b: 2 } };
const result = objectUtils.set(object, 'a.b', 3);
// Result: { a: { b: 3 } }
objectUtils.setWith(object, path, value, customizer)
This method is like objectUtils.set except that it accepts a customizer which is invoked to produce the assigned value.
Parameters
object
(Object): The object to modify.path
(Array|string): The path of the property to set.value
(*): The value to set.customizer
(Function): The customizer function.
Returns
- (Object): The modified object.
Example
const object = { a: { b: 2 } };
const customizer = (objValue, key, obj) => objValue * 2;
const result = objectUtils.setWith(object, 'a.b', 3, customizer);
// Result: { a: { b: 6 } }
objectUtils.toPairs(object)
Creates an array of own enumerable key-value pairs for an object.
Parameters
object
(Object): The object to convert.
Returns
- (Array): The array of key-value pairs.
Example
const object = { a: 1, b: 2 };
const result = objectUtils.toPairs(object);
// Result: [['a', 1], ['b', 2]]
objectUtils.toPairsIn(object)
Creates an array of all key-value pairs for an object, including inherited enumerable properties.
Parameters
object
(Object): The object to convert.
Returns
- (Array): The array of key-value pairs.
Example
const object = Object.create({ a: 1 });
object.b = 2;
const result = objectUtils.toPairsIn(object);
// Result: [['a', 1], ['b', 2]]
objectUtils.transform(object, iteratee, accumulator)
Iterates over own enumerable properties of an object, invoking the iteratee function for each property. The iteratee is invoked with three arguments: (value, key, object).
Parameters
object
(Object): The object to iterate over.iteratee
(Function): The iteratee function.accumulator
(*): The initial accumulator value.
Returns
- (*): The final accumulator value.
Example
const object = { a: 1, b: 2 };
const iteratee = (result, value, key) => {
result[key] = value * 2;
return result;
};
const result = objectUtils.transform(object, iteratee, {});
// Result: { a: 2, b: 4 }
objectUtils.unset(object, path)
Removes the property at path of an object.
Parameters
object
(Object): The object to modify.path
(Array|string): The path of the property to remove.
Returns
- (boolean): true if the property was removed, false otherwise.
Example
const object = { a: { b: 2 } };
const result = objectUtils.unset(object, 'a.b');
// Result: true
objectUtils.update(object, path, updater)
This method is like objectUtils.set except that it accepts an updater function.
Parameters
object
(Object): The object to modify.path
(Array|string): The path of the property to update.updater
(Function): The updater function.
Returns
- (Object): The modified object.
Example
const object = { a: { b: 2 } };
const updater = value => value * 2;
const result = objectUtils.update(object, 'a.b', updater);
// Result: { a: { b: 4 } }
objectUtils.updateWith(object, path, updater, customizer)
This method is like objectUtils.update except that it accepts a customizer which is invoked to produce the assigned value.
Parameters
object
(Object): The object to modify.path
(Array|string): The path of the property to update.updater
(Function): The updater function.customizer
(Function): The customizer function.
Returns
- (Object): The modified object.
Example
const object = { a: { b: 2 } };
const updater = value => value * 2;
const customizer = (objValue, key, obj) => objValue * 3;
const result = objectUtils.updateWith(object, 'a.b', updater, customizer);
// Result: { a: { b: 12 } }
objectUtils.values(object)
Creates an array of the own enumerable property values of an object.
Parameters
object
(Object): The object to query.
Returns
- (Array): The array of property values.
Example
const object = { a: 1, b: 2 };
const result = objectUtils.values(object);
// Result: [1, 2]
objectUtils.valuesIn(object)
Creates an array of all enumerable property values of an object.
Parameters
object
(Object): The object to query.
Returns
- (Array): The array of property values.
Example
const object = Object.create({ a: 1 });
object.b = 2;
const result = objectUtils.valuesIn(object);
// Result: [1, 2]