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

蹲街弑〆低调 提交于 2020-12-27 06:26:06

问题


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 something with a .slice() in it to remove the first n characters. How is this done to all keys in the object?

I should say that we are already importing Lodash in the project so I can use that.

So I need to turn this:

{
  'remove.this.string.a': "apple",
  'remove.this.string.b': "banana",
  'remove.this.string.c': "carrot",
  'remove.this.string.d': "diakon"
}

and turn it into:

{
  "a": "apple",
  "b": "banana",
  "c": "carrot",
  "d": "diakon"
}

回答1:


Use object.entries to get the keys and values. Loop over changing the key.

Changing the object directly

var obj = {
  'remove.this.string.a': "apple",
  'remove.this.string.b': "banana",
  'remove.this.string.c': "carrot",
  'remove.this.string.d': "diakon"
}

// Object.entries(obj).forEach(function(arr) {
//   var key = arr[0]
//   var value = arr[1]
//   delete obj[key]
//   obj[key.split(".").pop()] = value
// })
Object.entries(obj).forEach(([key, value]) => {
  delete obj[key]
  obj[key.split(".").pop()] = value
})

console.log(obj)

or reduce to create a new object

var obj = {
  'remove.this.string.a': "apple",
  'remove.this.string.b': "banana",
  'remove.this.string.c': "carrot",
  'remove.this.string.d': "diakon"
}

// const updated = Object.entries(obj).forEach(function(obj, arr) {
//   var key = arr[0]
//   var value = arr[1]
//   obj[key.split(".").pop()] = value
//   return obj
// }, {})
const updated = Object.entries(obj).reduce((obj, [key, value]) => {
  obj[key.split(".").pop()] = value
  return obj
}, {})

console.log(updated)



回答2:


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




回答3:


let obj = {
  'remove.this.string.a': "apple",
  'remove.this.string.b': "banana",
  'remove.this.string.c': "carrot",
  'remove.this.string.d': "diakon"
};

let transformed = Object.entries(obj).reduce((t, [key, value]) => {
  t[key.substr(19)] = value;
  return t;
}, {});

console.log(transformed);



回答4:


You could use the new Object.fromEntries along with Object.entries:

let remove = {
    this: {
        string: {}
    }
}

remove.this.string.a = "apple"
remove.this.string.b = "banana"
remove.this.string.c = "carrot"
remove.this.string.d = "diakon"

console.log(remove.this.string)

let fixed = Object.fromEntries(
    Object.entries(remove.this.string)
        .map(([key, val]) => [key, val])
)

console.log(fixed)

Result: { a: 'apple', b: 'banana', c: 'carrot', d: 'diakon' }

Update:

For keys that are all one string:

let remove = {
    'remove.this.string.a': 'apple',
    'remove.this.string.b': 'banana',
    'remove.this.string.c': 'carrot',
    'remove.this.string.d': 'diakon'
}

let fixed = Object.fromEntries(
    Object.entries(remove)
        .map(([key, val]) => [key.replace('remove.this.string.', ''), val])
)

console.log(fixed)

Result: { a: 'apple', b: 'banana', c: 'carrot', d: 'diakon' }




回答5:


Another approach that avoinds the need for Object.fromEntries(), would be to use Array.reduce() as shown:

const input = {
  'remove.this.string.a': "apple",
  'remove.this.string.b': "banana",
  'remove.this.string.c': "carrot",
  'remove.this.string.d': "diakon"
};

const output = Object.entries(input).reduce((result, [key, value]) => {
  
  /* Pluck last letter of key string */
  const letter = key.slice(-1);

  /* Insert letter key/value into result object */
  return { ...result, [letter] : value };
  
}, {});

console.log(output);


来源:https://stackoverflow.com/questions/57664795/remove-characters-from-all-keys-in-an-object-lodash-ok

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!