pickBy

Return a partial object copy containing properties for which a predicate (function) returns a truthy value.

Usage

var pickBy = require( '@stdlib/utils/pick-by' );

pickBy( obj, predicate )

Returns a partial object copy containing properties for which a predicate returns a truthy value.

function predicate( key, value ) {
    return ( value > 1 );
}

var obj1 = {
    'a': 1,
    'b': 2,
    'c': 3
};

var obj2 = pickBy( obj1, predicate );
// returns { 'b': 2, 'c': 3 }

Notes

  • The function returns a shallow copy.
  • The function only copies own properties. Hence, the function never copies inherited properties.

Examples

var pickBy = require( '@stdlib/utils/pick-by' );

function predicate( key, value ) {
    return ( typeof value === 'number' );
}

var obj1 = {
    'a': '1',
    'b': 2,
    'c': NaN,
    'd': null
};

var obj2 = pickBy( obj1, predicate );
// returns { 'b': 2, 'c': NaN }