back to Overview

_.filterObj()

Creates a new object that contains only those properties of the input object that have been approved by the filter function.
Util module only.

Syntax Variants

_.filterObj(obj, filterFunc)
_.filterObj(obj, filterFunc, ctx)

Parameters

obj
the object to use
callback
The callback function(key, value) to invoke for each property.
key
The name of the current property.
value
The value of the current property.
this
The given context. If not set, the object itself.
(callback return value)
true to include the property in the new object, false to omit it.
ctx (optional)
a context to pass to the callback as 'this'.
(return value)
the new object

Description

Creates a new object that contains only those properties of the input object that have been approved by the filter function.

If the callback function returns true, the property and its value are shallow-copied in the new object, otherwise it will be removed.

Example

Removing all values over 10 from an object:

var list = _.filterObj({a: 4, b: 22, c: 7, d: 2, e: 19}, function(key, value) {
    return value <= 10;
});

See also..

  • _.mapObj() can be used to modify the values og an object.

Comments

comments powered by Disqus

back to Overview

Functions