YuriGor / deepdash

eachDeep, filterDeep, findDeep, someDeep, omitDeep, pickDeep, keysDeep etc.. Tree traversal library written in Underscore/Lodash fashion
https://deepdash.io/
MIT License
274 stars 12 forks source link

deep map keys? #26

Closed masciugo closed 4 years ago

masciugo commented 5 years ago

How can I deeply transform keys with deepdash?

YuriGor commented 5 years ago

Hi, currently there is no such method, I'll think about adding it in future release, when I'll have a bit of free time. If you need it right now you can use reduceDeep instead

masciugo commented 5 years ago

For the sake of completeness, a specific method would be the best solution. But this is my implementation, which is quite satisfying

mapDeep( obj, (value, key, parentValue, context) => {
  if (_.isPlainObject(value)) {
    return _.mapKeys(value, (v,k) => {
      switch (k) {
        case 'id':
          return 'value'
        case 'name':
          return 'label'
        default:
          return k
      }
    })
  } else {
    return value
  }
})
YuriGor commented 4 years ago

Looks like I've chosen the wrong name for mapDeep. It actually should be mapValuesDeep, same as in lodash for one preserving object structure to be pair for mapKeysDeep requested above. And mapDeep is the correct name for 'collectDeep' planned in #42

YuriGor commented 4 years ago

done in v5,0,0 (that was tricky :sweat_smile:) mapKeysDeep also "old" mapDeep was renamed to mapValuesDeep and implemented again to return just array