Skip to content

Latest commit

 

History

History
63 lines (53 loc) · 2.16 KB

set.md

File metadata and controls

63 lines (53 loc) · 2.16 KB

Object Agent

A javascript library for working with objects

npm build coverage deps size vulnerabilities license


set(object, path, value) ⇒ object

Sets a nested value in an object. Keys in the path that don't exist at any point in the object will be created and added to the object once.

Returns: object - The mutated object.

Param Type Description
object object The object to mutate.
path string Dot delimited string.
value unknown The value to set at the end of the path.

Example

import { set } from 'object-agent';

const thing = {
    a: [{
        b: 'c'
    }, {
        b: 'd'
    }]
};

set(thing, 'a.1.b', 'e');
console.log(thing);
// => {
//    a: [{
//        b: 'c'
//    }, {
//        b: 'e'
//    }]
//}