Remove Characters from All Keys in an Object (Lodash OK)

前端 未结 5 1316
没有蜡笔的小新
没有蜡笔的小新 2021-01-27 19:46

I have a bothersome length of characters before all keys in this object. Since they are all the same, I would like to do a .map() or forEach() or some

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-27 20:15

    If you've already got lodash, _.mapKeys is what you're looking for. Here's an example of what you asked for directly (to just slice to 19 characters), but you could easily do a split or replace or whatever else you'd like:

    var _ = require('lodash')
    
    let data = {
      'remove.this.string.a': "apple",
      'remove.this.string.b': "banana",
      'remove.this.string.c': "carrot",
      'remove.this.string.d': "diakon"
    }
    _.mapKeys(data, (val, key) => key.slice(19))
    

    Here's a runkit: https://runkit.com/daedalus28/slice-keys

提交回复
热议问题