UNPKG

bit-bin

Version:

<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b

36 lines (30 loc) 827 B
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = filter; function _foreach() { const data = _interopRequireDefault(require("./foreach")); _foreach = function () { return data; }; return data; } /** * create a new array with all elements that pass the test implemented by the provided function. * @name filter * @param {object} obj object or array to iterate * @param {function} cb callback function to invoke * @example * ```js * filter({ a: 1, b: 2, c: 3 }, (val, key) => val === 1) // => { a: 1 } * ``` */ function filter(obj, cb) { const newObj = {}; (0, _foreach().default)(obj, (val, key) => { if (cb(val, key)) newObj[key] = val; }); return obj; }