js-select-from-object

A JS util to select a property from an object, without throwing errors.

Usage no npm install needed!

<script type="module">
  import jsSelectFromObject from 'https://cdn.skypack.dev/js-select-from-object';
</script>

README

js-select-from-object

A JS util to select a property from an object, and if it doesn't exist, return a default value, instead of throwing an error or returning an undefined value.

npm version

Installation

To start using this library, first install the package:

npm install js-select-from-object --save

Usage

An example of how it can be used:

import select from 'js-select-from-object';

const o = {
  foo: 'bar',
  xxx: {
    yyy: 'zzz'
  }
};

select(o, 'foo', 'wut?'); // => returns 'bar'
select(o, 'xxx.yyy', 'wut?'); // => returns 'zzz'
select(o, 'aaa.bbb'); // => returns null
select(o, 'aaa.bbb', 'wut?'); // => returns 'wut?'

Todo

  • Allow for selection of arrays within objects