UNPKG

577 BJavaScriptView Raw
1var each = require('./each')
2
3/**
4 * 根据回调过滤数据
5 *
6 * @param {Object} obj 对象/数组
7 * @param {Function} iterate(item, index, obj) 回调
8 * @param {Object} context 上下文
9 * @return {Object}
10 */
11function filter (obj, iterate, context) {
12 var result = []
13 if (obj && iterate) {
14 if (obj.filter) {
15 return obj.filter(iterate, context)
16 }
17 each(obj, function (val, key) {
18 if (iterate.call(context, val, key, obj)) {
19 result.push(val)
20 }
21 })
22 }
23 return result
24}
25
26module.exports = filter