0.0.4 • Published 6 years ago

@mqschwanda/safe-get-nested-object v0.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

safe-get-nested-object

get

source
/**
 * safely access an object's deeply nested values
 * @param  {[String]} path the chain of keys we are trying to access
 * @param  {Object} object the object we want to retrieve the values from
 * @return {Object, Array, String} nested value
 */
const get = path => object =>
  path.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, object);
usage
import get from '@mqschwanda/safe-get-nested-object';

const object = { nested: { msgs: ['it worked!'] } };
const getMsg = get(['nested', 'msgs', 0]);

console.log(getMsg(object)); // --> 'it worked!'
console.log(getMsg({})); // --> null
0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago