1.0.6 • Published 7 years ago

nested-value v1.0.6

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

Nested-value

Get the value from nested object

Build Status Known Vulnerabilities

Install

npm install nested-value

Test

npm test

Usage

Require nested-values

const nestedValue = require('nested-value');

You can get nested value from an object:

const nestObj =  { a: { b: { c: "i'm here" } } };

const nestedValue = nestedValue(nestObj, 'a.b.c'); //=> "i'm here"
//or
const nestedValue = nestedValue(nestObj, ['a','b','c']); //=> "i'm here"

Or value inside an nested array

const nestArray = [[['a'], { b: 'valueB', c: [{ d: 'valueD' }] }]];
const nestedValue = nestedValue(nestArray, '0.1.c.0.d');
// => "valueD"

You can also pass a callback:

const nestObj =  { a: { b: { c: "i'm here" } } };

const newObject = nestedValue(nestObj, 'a.b.c', function (val){
    return { foo: val }
});
//=> { foo: "i'm here" }

API

nestedValue(source, structure, callback)

source

Type: object

Source object to get nested value.

structure

Type: array or string

  • String: separating property by '.' -> "foo.bar.foo"
  • Array: fill property in normal array -> [0, "bar", 1]

callback

Type: function

The callback will trigger after the property is obtained.

  • It has one parameter callback(value).

LICENSE

MIT

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.1.0

8 years ago