Map over object preserving keys

后端 未结 12 1419
再見小時候
再見小時候 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: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.

提交回复
热议问题