Map over object preserving keys

后端 未结 12 1452
再見小時候
再見小時候 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
      }, {})
    }
    

提交回复
热议问题