A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/yangg/dot2val below:

yangg/dot2val: Set or get a value within a deeply nested object using `dot' notation

Set or get a value within a deeply nested object using `dot' notation

npm install --save dot2val
var dot2val = require('dot2val');

Given:

var obj = {
    prop1: {
        arr: ['a', 'b', 'c']
      , str: 'Hello'
    }
  , prop2: {
        arr: [ { nested: 'Universe' } ]
      , str: 'Hello again!'
    }
}

var arr = [ { foo: 'bar' } ];

Expect:

dot2val.get(obj, 'prop1.str'); // => "Hello"
dot2val.get(obj, 'prop1.arr.2'); // => "c"
dot2val.get(obj, 'prop2.arr.0.nested'); // => "Universe"

dot2val.get(arr, '0.foo'); // => "bar"

dot2val.get(undefined, 'doesnt.matter'); // => undefined
dot2val.get({}, 'doesnt.exist'); // => undefined
dot2val.get({}, 'doesnt.exist', 'default value'); // => "default value"
dot2val.set(obj, 'hello.universe', 'properties');
dot2val.set(obj, 'hello1', 'universe');
dot2val.set(obj, 'prop2');        // delete obj['prop2']

Try dot2val in your browser

MIT


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4