The lodash method update exported as a module for updating object properties at specified paths using an updater function.
npx @tessl/cli install tessl/npm-lodash--update@4.10.0The lodash.update method exported as a Node.js module for updating object properties at specified paths using an updater function. This package provides deep property modification capabilities within nested objects and arrays through dot notation or bracket notation paths, with the updater function receiving the current value and returning the new value. The method mutates the original object and creates any missing intermediate properties along the path.
npm install lodash.updatevar update = require('lodash.update');var update = require('lodash.update');
// Basic property update with updater function
var object = { 'a': [{ 'b': { 'c': 3 } }] };
update(object, 'a[0].b.c', function(n) { return n * n; });
console.log(object.a[0].b.c);
// => 9
// Creating missing properties with updater function
update(object, 'x[0].y.z', function(n) { return n ? n + 1 : 0; });
console.log(object.x[0].y.z);
// => 0Updates object properties at specified paths using an updater function. The method mutates the original object and creates any missing intermediate properties along the path.
/**
* This method is like `_.set` except that accepts `updater` to produce the
* value to set. Use `_.updateWith` to customize `path` creation. The `updater`
* is invoked with one argument: (value).
*
* **Note:** This method mutates `object`.
*
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {Function} updater The function to produce the updated value.
* @returns {Object} Returns `object`.
*/
function update(object, path, updater);Parameters:
object (Object): The object to modifypath (Array|string): The path of the property to set. Can use dot notation (e.g., 'a.b.c'), bracket notation (e.g., 'a[0].b'), or array format (['a', '0', 'b'])updater (Function): The function to produce the updated value. Receives the current value at the path as its argumentReturns:
objectBehavior:
undefined if the path doesn't exist)Usage Examples:
var update = require('lodash.update');
// Simple property update
var data = { count: 5 };
update(data, 'count', function(n) { return n + 1; });
console.log(data.count); // => 6
// Nested object update
var user = { profile: { stats: { score: 100 } } };
update(user, 'profile.stats.score', function(score) { return score * 2; });
console.log(user.profile.stats.score); // => 200
// Array element update
var collection = { items: [{ value: 10 }, { value: 20 }] };
update(collection, 'items[1].value', function(val) { return val + 5; });
console.log(collection.items[1].value); // => 25
// Creating new nested structure
var empty = {};
update(empty, 'a.b[0].c', function(val) { return val || 'default'; });
console.log(empty.a.b[0].c); // => 'default'
// Complex object transformation
var state = {
users: {
john: { posts: 5, active: true }
}
};
update(state, 'users.john.posts', function(posts) {
return posts + 1;
});
console.log(state.users.john.posts); // => 6
// Non-function updater (no change occurs)
var test = { value: 10 };
update(test, 'value', 'new value'); // Non-function updater
console.log(test.value); // => 10 (unchanged)Path Format Examples:
// Dot notation for object properties
update(obj, 'a.b.c', updater);
// Bracket notation for array indices
update(obj, 'a[0].b[1]', updater);
// Mixed notation
update(obj, 'users[0].profile.settings.theme', updater);
// Array format
update(obj, ['a', '0', 'b'], updater);
// Numeric keys as strings
update(obj, 'matrix.0.1', updater);Error Handling:
The function handles various edge cases gracefully:
object is null or undefined{} or arrays [] based on the next key in the pathType Coercion:
Path segments are automatically converted to appropriate types: