Home

function: set


set(object: Object, path: Array, string, value: *): 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. Use `setWith` to customize `path` creation. **Note:** This method mutates `object`.

Example(s):

const 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

Params:

Name Type Attribute Description
object The object to modify.
path The path of the property to set.
value The value to set.

Returns:

Object

  {
    "comment": "/**\r\n * Sets the value at `path` of `object`. If a portion of `path` doesn't exist,\r\n * it's created. Arrays are created for missing index properties while objects\r\n * are created for all other missing properties. Use `setWith` to customize\r\n * `path` creation.\r\n *\r\n * **Note:** This method mutates `object`.\r\n *\r\n * @since 3.7.0\r\n * @category Object\r\n * @param {Object} object The object to modify.\r\n * @param {Array|string} path The path of the property to set.\r\n * @param {*} value The value to set.\r\n * @returns {Object} Returns `object`.\r\n * @see has, hasIn, get, unset\r\n * @example\r\n *\r\n * const object = { 'a': [{ 'b': { 'c': 3 } }] }\r\n *\r\n * set(object, 'a[0].b.c', 4)\r\n * console.log(object.a[0].b.c)\r\n * // => 4\r\n *\r\n * set(object, ['x', '0', 'y', 'z'], 5)\r\n * console.log(object.x[0].y.z)\r\n * // => 5\r\n */",
    "meta": {
        "range": [
            867,
            970
        ],
        "filename": "set.js",
        "lineno": 30,
        "path": "C:\\Users\\beaujeup\\projects\\jsdoc-template\\examples\\lodash\\lodash-repo",
        "code": {
            "id": "astnode100008065",
            "name": "set",
            "type": "FunctionDeclaration",
            "paramnames": [
                "object",
                "path",
                "value"
            ]
        }
    },
    "description": "Sets the value at `path` of `object`. If a portion of `path` doesn't exist,\rit's created. Arrays are created for missing index properties while objects\rare created for all other missing properties. Use `setWith` to customize\r`path` creation.\r\r**Note:** This method mutates `object`.",
    "since": "3.7.0",
    "tags": [
        {
            "originalTitle": "category",
            "title": "category",
            "text": "Object",
            "value": "Object"
        }
    ],
    "params": [
        {
            "type": {
                "names": [
                    "Object"
                ]
            },
            "description": "The object to modify.",
            "name": "object"
        },
        {
            "type": {
                "names": [
                    "Array",
                    "string"
                ]
            },
            "description": "The path of the property to set.",
            "name": "path"
        },
        {
            "type": {
                "names": [
                    "*"
                ]
            },
            "description": "The value to set.",
            "name": "value"
        }
    ],
    "returns": [
        {
            "type": {
                "names": [
                    "Object"
                ]
            },
            "description": "Returns `object`."
        }
    ],
    "see": [
        "has, hasIn, get, unset"
    ],
    "examples": [
        "const object = { 'a': [{ 'b': { 'c': 3 } }] }\r\rset(object, 'a[0].b.c', 4)\rconsole.log(object.a[0].b.c)\r// => 4\r\rset(object, ['x', '0', 'y', 'z'], 5)\rconsole.log(object.x[0].y.z)\r// => 5"
    ],
    "name": "set",
    "longname": "set",
    "kind": "function",
    "scope": "global",
    "___id": "T000002R000742",
    "___s": true,
    "filepath": "set.js"
}