slice-keys

Slice keys and values from an object

Usage no npm install needed!

<script type="module">
  import sliceKeys from 'https://cdn.skypack.dev/slice-keys';
</script>

README

slice-keys

Returns a subset of the given object with only the requested attributes.

sliceKeys({x:1, y:2, z:3}, ['x','z']) //=> {x:1, z:3}
sliceKeys({x:1, y:2, z:3}, 'x', 'z')  //=> {x:1, z:3}

This is particularly useful when filtering user input:

params = {name: "Monty Pontihew", admin: true};
allow  = ['name', 'age'];

user.update(sliceKeys(params, allow)); // Only name will be passed in

Installation

Install with npm:

npm install slice-keys

Tests

Test with mocha:

mocha

Enjoy.


Adam Sanderson