get-first

Pass an array of object paths (e.g. `a.b.c`) and get the first non-undefined value if the property exists, or null if it does not.

Usage no npm install needed!

<script type="module">
  import getFirst from 'https://cdn.skypack.dev/get-first';
</script>

README

get-first NPM version

Pass an array of object paths (e.g. a.b.c) and get the first non-undefined value if the property exists, or null if it does not.

Install

Install with npm:

$ npm i get-first

Run tests

npm test

Usage

var getFirst = require('get-first');

getFirst({a: 'b'}, 'a');
//=> 'b'

getFirst({a: 'b'}, ['a']);
//=> 'b'

getFirst({a: {b: 'c'}}, 'a.b');
//=> 'c'

getFirst({a: {b: 'c'}}, ['a.b']);
//=> 'c'

getFirst({a: {b: 'c'}, x: {b: 'd'}}, ['a.b', 'x.b']);
//=> 'c'

getFirst({a: {b: 'c'}, x: {b: 'd'}}, ['x.b', 'a.b']);
//=> 'd'

getFirst({a: {b: {c: 'd'}}}, ['a.b', 'x.y.z']);
//=> {c: 'd'}

getFirst({a: {b: {c: 'd'}, e: 'f'}}, ['a.c', 'a.e']);
//=> 'f'

Related

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Jon Schlinkert

License

Copyright © 2016 Jon Schlinkert Released under the MIT license.


This file was generated by verb on January 18, 2016.