# \_.set

**语法：**

```javascript
_.set(object, path, value)
```

**源代码链接：**[source](https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L13672)

**npm包链接：**[npm package](https://www.npmjs.com/package/lodash.set)

**描述：**

设置`object`对象中对应`path`属性路径上的值，如果`path`不存在，则创建。 缺少的索引属性会创建为数组，而缺少的属性会创建为对象。 使用[`_.setWith`](https://lodash.com/docs/4.17.10#setWith)定制`path`创建。

> **Note:** 这个方法会改变`object`。

**开始版本：**&#x33;.7.0

**参数：**

* `object (Object)`: 要修改的对象。
* `path (Array|string)`: 要设置的对象路径。
* `value (*)`: 要设置的值。

**返回值：**

* `(Object)`: 返回 object。

**例子：**

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

_.set(object, 'a[0].b.c', 4);
console.log(object.a[0].b.c);
// => 4

_.set(object, ['x', '0', 'y', 'z'], 5);
console.log(object.x[0].y.z);
// => 5
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lodash.shujuwajue.com/object/set.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
