Naughty Programmer's Madness

    object-dig

    0.1.3 • Public • Published

    object-dig

    Build Status

    This allow you to use method like Ruby's hash#dig in JavaScript.

    http://ruby-doc.org/core-2.3.0_preview1/Hash.html#method-i-dig

    Retrieves the value object corresponding to the each key objects repeatedly.

    Install

    $ npm install --save object-dig
    

    Usage

    var dig = require('object-dig');
     
    var object = { a: { b: { c: 'c' } } };
     
    dig(object, 'a', 'b');
    // => { c: 'c' }
     
    dig(object, 'a', 'b', 'c');
    // => 'c'
     
    dig(object, 'a', 'unknownProp', 'c');
    // =>undefined

    and you can give function object to dig. Function object's argument is result of just before evaluating.

    dig(object, 'a', 'b', 'c', (val) => `${val} was changed`);
    // => 'c was changed'
     
    dig(object, 'a', 'b', 'c', (val) => `${val} was changed`, (val) => `${val} more`);
    // => 'c was changed more'

    License

    MIT © joe-re

    Install

    npm i object-dig

    DownloadsWeekly Downloads

    4,507

    Version

    0.1.3

    License

    MIT

    Unpacked Size

    4.85 kB

    Total Files

    7

    Last publish

    Collaborators

    • joe-re