Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including trim-keys with all npm packages installed. Try it out:

var trimKeys = require("trim-keys")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

trim-keys v0.0.4

Keep or remove certain keys from an object.

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 } }

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free