get, set and remove javascript object values using dynamic keys
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
.editorconfig
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
index.js
package-lock.json
package.json

README.md

deepval

Get, Set and Remove object values using dot-delimited key strings

deepval(<object to inspect>, <string path> or <array of string keys>, [value to set], [boolean: remove])

or:

deepval.get(obj, path);
deepval.set(obj, path, value);
deepval.del(obj, path);

examples:

var obj = {
  a: {
    b: {
      c: 'deep'
    },
    d: ['foo'],
    e: ['bar', ['aaa', 'bbb']]
  }
};

// get a value
console.log(deepval(obj, 'a.b.c'));   // 'deep'
// set a value
deepval(obj, 'a.b.c', 'something');
console.log(deepval(obj, 'a.b.c'));   // 'something'
// remove a value
deepval(obj, 'a.b.c', null, true);
console.log(deepval(obj, 'a.b.c'));   // undefined
// read array values
console.log(deepval(obj, 'a.d.0'));   // 'foo'
console.log(deepval(obj, ['a', 'd', 0]));   // 'foo'
console.log(deepval(obj, 'a.e.1.0')); // 'aaa'

deepval provides a utility function dotpath to create a dot-delimited string from a set of passed arguments:

console.log(deepval.dotpath('hello', a.b.0))  //  'hello.foo'