🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

dot2val

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dot2val

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

latest
Source
npmnpm
Version
1.2.2
Version published
Maintainers
1
Created
Source

dot2val

Build Status Test Coverage Code Climate npm:

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

Installation

npm install --save dot2val

Usage

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:

get

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"

set

dot2val.set(obj, 'hello.universe', 'properties');
dot2val.set(obj, 'hello1', 'universe');
dot2val.set(obj, 'prop2');        // delete obj['prop2']

Try dot2val in your browser

License

MIT

Keywords

util

FAQs

Package last updated on 12 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts