trim-keys

Keep or remove certain keys from an object.

Usage no npm install needed!

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

README

trim-keys

Keep or remove certain keys from an object.

Installation

npm install trim-keys

Environment Support

trim-keys has been tested in Node, IE9+, Chrome, Firefox, and Opera.

Usage

// CommonJS
var trim = require('trim-keys');
// AMD
require(['trim-keys'], function(trim) { ... });
// Script Tag
var trim = window.trim;

API

trim(obj, keyMap)

obj

type: Object

The object to trim. This object will be modified, so if you want to retain your original object, you'll need to pass in a copy of that object.

keyMap

type: Object

This object defines what properties to keep, or what properties to remove. It takes the following form:

{ field1: <boolean>, field2: <boolean> ... }

The <boolean> value can be any of the following:

  • 1 or true to include the field.
  • 0 or false to exclude the field.

A keyMap cannot contain both include and exclude specifications. An error will be thrown if all of the keys are not the same value.

Example

var trim = require('trim-keys');

var person = { name: 'John Doe', age: 40, height: { ft: 5, in: 10 } };

trim(person, { name: 1 })
// person === { name: 'John Doe' }
var trim = require('trim-keys');

var person = { name: 'John Doe', age: 40, height: { ft: 5, in: 10 } };

trim(person, { age: 0, height: { in: 0 } });
// person === { name: 'John Doe', height: { ft: 5 } }