or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash-set

The lodash method _.set exported as a module for setting values at deep object paths with automatic intermediate object creation.

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

To install, run

npx @tessl/cli install tessl/npm-lodash-set@4.3.0

index.mddocs/

lodash.set

The lodash method _.set exported as a standalone Node.js module for setting values at deep object paths with automatic intermediate object creation. This package allows developers to safely set nested properties using string or array notation without worrying about missing intermediate objects or arrays.

Package Information

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

Core Imports

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

For ES6 modules (via transpilation or bundler):

import set from 'lodash.set';

Basic Usage

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

// Create an object to modify
var object = { 'a': [{ 'b': { 'c': 3 } }] };

// Set a nested value using dot notation
set(object, 'a[0].b.c', 4);
console.log(object.a[0].b.c);
// => 4

// Set values in missing paths - creates intermediate objects/arrays automatically
set(object, ['x', '0', 'y', 'z'], 5);
console.log(object.x[0].y.z);
// => 5

// Works with dot notation strings
set(object, 'deep.nested.property', 'hello');
console.log(object.deep.nested.property);
// => 'hello'

Capabilities

Set Function

Sets the value at path of object. If a portion of path doesn't exist, it's created. Arrays are created for missing index properties while objects are created for all other missing properties.

Note: This method mutates object.

/**
 * Sets the value at `path` of `object`. If a portion of `path` doesn't exist,
 * it's created. Arrays are created for missing index properties while objects
 * are created for all other missing properties.
 *
 * @param {Object} object - The object to modify
 * @param {Array|string} path - The path of the property to set
 * @param {*} value - The value to set
 * @returns {Object} Returns `object`
 */
function set(object, path, value);

Parameters

  • object (Object): The object to modify. If null or undefined, returns the object unchanged.
  • path (Array|string): The path of the property to set. Can be:
    • Dot notation string: 'a.b.c'
    • Bracket notation string: 'a[0].b.c' or 'a["key"].b'
    • Mixed notation: 'a[0].b.c.d'
    • Array of path segments: ['a', '0', 'b', 'c']
  • value (*): The value to set at the specified path.

Return Value

  • Type: Object
  • Description: Returns the modified object. The original object is mutated.

Path Creation Behavior

  • Missing objects: Created for string keys ('a.b.c' creates nested objects)
  • Missing arrays: Created for numeric indices ('a[0].b' creates array at a)
  • Mixed structures: Intelligently creates objects vs arrays based on path segment type
  • Deep paths: Automatically creates entire path structure as needed

Usage Examples

Basic path setting:

var obj = {};
set(obj, 'user.name', 'John');
// obj becomes: { user: { name: 'John' } }

Array index creation:

var obj = {};
set(obj, 'items[0].id', 123);
// obj becomes: { items: [{ id: 123 }] }

Complex nested structures:

var obj = {};
set(obj, 'users[0].profile.settings.theme', 'dark');
// obj becomes: { users: [{ profile: { settings: { theme: 'dark' } } }] }

Using array path notation:

var obj = {};
set(obj, ['data', 'results', '0', 'value'], 42);
// obj becomes: { data: { results: [{ value: 42 }] } }

Handling null/undefined objects:

set(null, 'a.b.c', 123);
// => null (no operation performed)

set(undefined, 'a.b.c', 123);
// => undefined (no operation performed)

Edge Cases and Notes

  • Object mutation: The original object is always modified (mutated) if it's not null/undefined
  • Existing values: Overwrites existing values at the specified path
  • Type coercion: Path segments are used as-is for object keys
  • Performance: Uses internal caching and memoization for optimal performance
  • Array indices: Numeric strings in paths create array indices, other strings create object properties