Skip to content

addhome2001/nested-value

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Get the value from nested object

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •