Map over object preserving keys

后端 未结 12 1417
再見小時候
再見小時候 2020-12-07 14:41

The map function in underscore.js, if called with a javascript object, returns an array of values mapped from the object\'s values.

_.map({one:          


        
相关标签:
12条回答
  • 2020-12-07 14:47

    I know it's been a long time, but still the most obvious solution via fold (aka reduce in js) is missing, for the sake of completeness i'll leave it here:

    function mapO(f, o) {
      return Object.keys(o).reduce((acc, key) => {
        acc[key] = f(o[key])
        return acc
      }, {})
    }
    
    0 讨论(0)
  • 2020-12-07 14:56

    I know this is old, but now Underscore has a new map for objects :

    _.mapObject(object, iteratee, [context]) 
    

    You can of course build a flexible map for both arrays and objects

    _.fmap = function(arrayOrObject, fn, context){
        if(this.isArray(arrayOrObject))
          return _.map(arrayOrObject, fn, context);
        else
          return _.mapObject(arrayOrObject, fn, context);
    }
    
    0 讨论(0)
  • 2020-12-07 14:58

    _.map returns an Array, not an Object.

    If you want an object you're better off using a different function, like each; if you really want to use map you could do something like this:

    Object.keys(object).map(function(value, index) {
       object[value] *= 3;
    })
    

    but that is confusing, when seeing map one would expect to have an array as result and then make something with it.

    0 讨论(0)
  • 2020-12-07 15:02

    _.map using lodash like loop to achieve this

     var result={};
    _.map({one: 1, two: 2, three: 3}, function(num, key){ result[key]=num * 3; });
    console.log(result)
    
    //output
    {one: 1, two: 2, three: 3}
    

    Reduce is clever looks like above answare

    _.reduce({one: 1, two: 2, three: 3}, function(result, num, key) {
      result[key]=num * 3
      return result;
    }, {});
    
    //output
    {one: 1, two: 2, three: 3}
    
    0 讨论(0)
  • 2020-12-07 15:03

    I think you want a mapValues function (to map a function over the values of an object), which is easy enough to implement yourself:

    mapValues = function(obj, f) {
      var k, result, v;
      result = {};
      for (k in obj) {
        v = obj[k];
        result[k] = f(v);
      }
      return result;
    };
    
    0 讨论(0)
  • 2020-12-07 15:03

    A mix fix for the underscore map bug :P

    _.mixin({ 
        mapobj : function( obj, iteratee, context ) {
            if (obj == null) return [];
            iteratee = _.iteratee(iteratee, context);
            var keys = obj.length !== +obj.length && _.keys(obj),
                length = (keys || obj).length,
                results = {},
                currentKey;
            for (var index = 0; index < length; index++) {
              currentKey = keys ? keys[index] : index;
              results[currentKey] = iteratee(obj[currentKey], currentKey, obj);
            }
            if ( _.isObject( obj ) ) {
                return _.object( results ) ;
            } 
            return results;
        }
    }); 
    

    A simple workaround that keeps the right key and return as object It is still used the same way as i guest you could used this function to override the bugy _.map function

    or simply as me used it as a mixin

    _.mapobj ( options , function( val, key, list ) 
    
    0 讨论(0)
提交回复
热议问题