or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md
tile.json

index.mddocs/

0

# lodash.set

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: lodash.set

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install lodash.set`

10

11

## Core Imports

12

13

```javascript

14

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

15

```

16

17

For ES6 modules (via transpilation or bundler):

18

19

```javascript

20

import set from 'lodash.set';

21

```

22

23

## Basic Usage

24

25

```javascript

26

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

27

28

// Create an object to modify

29

var object = { 'a': [{ 'b': { 'c': 3 } }] };

30

31

// Set a nested value using dot notation

32

set(object, 'a[0].b.c', 4);

33

console.log(object.a[0].b.c);

34

// => 4

35

36

// Set values in missing paths - creates intermediate objects/arrays automatically

37

set(object, ['x', '0', 'y', 'z'], 5);

38

console.log(object.x[0].y.z);

39

// => 5

40

41

// Works with dot notation strings

42

set(object, 'deep.nested.property', 'hello');

43

console.log(object.deep.nested.property);

44

// => 'hello'

45

```

46

47

## Capabilities

48

49

### Set Function

50

51

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.

52

53

**Note:** This method mutates `object`.

54

55

```javascript { .api }

56

/**

57

* Sets the value at `path` of `object`. If a portion of `path` doesn't exist,

58

* it's created. Arrays are created for missing index properties while objects

59

* are created for all other missing properties.

60

*

61

* @param {Object} object - The object to modify

62

* @param {Array|string} path - The path of the property to set

63

* @param {*} value - The value to set

64

* @returns {Object} Returns `object`

65

*/

66

function set(object, path, value);

67

```

68

69

#### Parameters

70

71

- **object** (`Object`): The object to modify. If `null` or `undefined`, returns the object unchanged.

72

- **path** (`Array|string`): The path of the property to set. Can be:

73

- Dot notation string: `'a.b.c'`

74

- Bracket notation string: `'a[0].b.c'` or `'a["key"].b'`

75

- Mixed notation: `'a[0].b.c.d'`

76

- Array of path segments: `['a', '0', 'b', 'c']`

77

- **value** (`*`): The value to set at the specified path.

78

79

#### Return Value

80

81

- **Type**: `Object`

82

- **Description**: Returns the modified `object`. The original object is mutated.

83

84

#### Path Creation Behavior

85

86

- **Missing objects**: Created for string keys (`'a.b.c'` creates nested objects)

87

- **Missing arrays**: Created for numeric indices (`'a[0].b'` creates array at `a`)

88

- **Mixed structures**: Intelligently creates objects vs arrays based on path segment type

89

- **Deep paths**: Automatically creates entire path structure as needed

90

91

#### Usage Examples

92

93

**Basic path setting:**

94

95

```javascript

96

var obj = {};

97

set(obj, 'user.name', 'John');

98

// obj becomes: { user: { name: 'John' } }

99

```

100

101

**Array index creation:**

102

103

```javascript

104

var obj = {};

105

set(obj, 'items[0].id', 123);

106

// obj becomes: { items: [{ id: 123 }] }

107

```

108

109

**Complex nested structures:**

110

111

```javascript

112

var obj = {};

113

set(obj, 'users[0].profile.settings.theme', 'dark');

114

// obj becomes: { users: [{ profile: { settings: { theme: 'dark' } } }] }

115

```

116

117

**Using array path notation:**

118

119

```javascript

120

var obj = {};

121

set(obj, ['data', 'results', '0', 'value'], 42);

122

// obj becomes: { data: { results: [{ value: 42 }] } }

123

```

124

125

**Handling null/undefined objects:**

126

127

```javascript

128

set(null, 'a.b.c', 123);

129

// => null (no operation performed)

130

131

set(undefined, 'a.b.c', 123);

132

// => undefined (no operation performed)

133

```

134

135

#### Edge Cases and Notes

136

137

- **Object mutation**: The original object is always modified (mutated) if it's not null/undefined

138

- **Existing values**: Overwrites existing values at the specified path

139

- **Type coercion**: Path segments are used as-is for object keys

140

- **Performance**: Uses internal caching and memoization for optimal performance

141

- **Array indices**: Numeric strings in paths create array indices, other strings create object properties