JS - deep map function

后端 未结 8 524
挽巷
挽巷 2020-12-06 04:48

Underscore.js has a very useful map function.

_.map([1, 2, 3], function(num){ return num * 3; });
=> [3, 6, 9]
_.map({one: 1, two: 2, three: 3         


        
8条回答
  •  一生所求
    2020-12-06 05:23

    Here's a Lodash solution using transform

    function deepMap(obj, iterator, context) {
        return _.transform(obj, function(result, val, key) {
            result[key] = _.isObject(val) /*&& !_.isDate(val)*/ ?
                                deepMap(val, iterator, context) :
                                iterator.call(context, val, key, obj);
        });
    }
    
    _.mixin({
       deepMap: deepMap
    });
    

提交回复
热议问题