object-set

1.0.1 • Public • Published

object-set

Sets the value at path of object.

MIT License

build:? coverage:?

Install

$ npm install --save object-set 

Usage

For more use-cases see the tests

var set    = require('object-set');
var object = { 'a': [{ 'b': { 'c': 3 } }] };
 
set(object, 'a[0].b.c', 4);
// => { 'a': [{ 'b': { 'c': 4 } }] }
 
set({ foo: { bar: 'baz' } }, 'foo.arr[0]', 3);
// => { foo: {bar: 'baz', arr: [3] } }
 
set({ foo: { bar: 'baz' } }, 'foo.arr[1]', 3);
// => { foo: {bar: 'baz', arr: [, 3] } }
 
set({ foo: { bar: 'baz' } }, 'foo.obj.key', 3);
// => { foo: {bar: 'baz', obj: { key: 3 } } }
 
set(null, 'foo.obj.key', 3);
// => null
 
set({ foo: 1 }, 'foo.obj.key', 3);
// => { foo: 1 }

Related

  • object-unset - Removes the property at path of object.
  • object-at - Get object's property according to the path.
  • object-has - Checks if path is a direct property of object.
  • to-path - Converts string to a property path array.

Contributing

Pull requests and stars are highly welcome.

For bugs and feature requests, please create an issue.

/object-set/

    Package Sidebar

    Install

    npm i object-set

    Weekly Downloads

    592

    Version

    1.0.1

    License

    MIT

    Last publish

    Collaborators

    • bubkoo