1 | var each = require('./each')
|
2 | var isFunction = require('./isFunction')
|
3 | var property = require('./property')
|
4 |
|
5 | /**
|
6 | * 指定方法后的返回值组成的新对象
|
7 | *
|
8 | * @param {Object} obj 对象/数组
|
9 | * @param {Function} iterate(item, index, obj) 回调
|
10 | * @param {Object} context 上下文
|
11 | * @return {Object}
|
12 | */
|
13 | function objectMap (obj, iterate, context) {
|
14 | var result = {}
|
15 | if (obj) {
|
16 | if (iterate) {
|
17 | if (!isFunction(iterate)) {
|
18 | iterate = property(iterate)
|
19 | }
|
20 | each(obj, function (val, index) {
|
21 | result[index] = iterate.call(context, val, index, obj)
|
22 | })
|
23 | } else {
|
24 | return obj
|
25 | }
|
26 | }
|
27 | return result
|
28 | }
|
29 |
|
30 | module.exports = objectMap
|