1.0.6 • Published 7 years ago
nested-value v1.0.6
Nested-value
Get the value from nested object
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