or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--update

The lodash method update exported as a module for updating object properties at specified paths using an updater function.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.update@4.10.x

To install, run

npx @tessl/cli install tessl/npm-lodash--update@4.10.0

index.mddocs/

lodash.update

The 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.

Package Information

  • Package Name: lodash.update
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.update

Core Imports

var update = require('lodash.update');

Basic Usage

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);
// => 0

Capabilities

Object Property Update

Updates 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 modify
  • path (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 argument

Returns:

  • (Object): Returns the modified object

Behavior:

  • Mutation: Modifies the original object in-place
  • Path Creation: Creates missing intermediate properties along the path
  • Array Handling: Supports array indices in paths using bracket notation or numeric string keys
  • Updater Invocation: The updater function receives the current value at the specified path (or 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:

  • Returns the original object unchanged if object is null or undefined
  • Creates intermediate properties as plain objects {} or arrays [] based on the next key in the path
  • Non-function updater values are converted to identity functions, resulting in no changes to the object (the current value is returned unchanged)

Type Coercion:

Path segments are automatically converted to appropriate types:

  • Numeric strings become array indices when the target is an array
  • String keys are used for object properties
  • Symbol keys are preserved as-is and supported for object properties