1.0.1 • Published 7 years ago

meeseeks-get v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

meeseeks-get

I'm Mr. Meeseeks! Look at me! I can get any nested value for you from the most complex objects. Mister Meeseeks

Simple tool for accessing nested object values Implemented using Array.prototype.reduce

Install with npm install meeseeks-get --save

Require with

var meeseeksGet = require('meeseeks-get')

import meeseeksGet from 'meeseeks-get'

Function signature

meeseeksGet(object, path, defaultValue) where path can be both an array of values (keys) for the object or a string with . separated keys (valid path values: ['a', 'b'], 'a.b')

Array objects can be accessed via index directly or with an index in brackets meeseeksGet(object, '[0]') === meeseeksGet(object, '0')

Few example usages:

var complexObject = {
    mainActor: {
        name: 'Mister Meeseeks'
    },
    friends: [
        {
            name: 'Rick'
        },
        {
            name: 'Morty'
        }
    ]
}
// with string path
meeseeksGet(complexObject, 'mainActor.name') === 'Mister Meeseeks'
meeseeksGet(complexObject, 'friends.0.name') === 'Rick'
meeseeksGet(complexObject, 'friends.[0].name') === 'Rick'

// with array path
meeseeksGet(complexObject, ['mainActor', 'name']) === 'Mister Meeseeks'
meeseeksGet(complexObject, ['friends', 0, 'name']) === 'Rick'

// with default value
meeseeksGet(complexObject, 'mainActor.race', 'imaginary creature') === 'imaginary creature'