简体   繁体   English

从对象的所有键中删除字符(Lodash OK)

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

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. 由于它们都是相同的,所以我想做一个.map()forEach()或其中带有.slice()东西,以删除前n个字符。 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. 我应该说我们已经在项目中导入了Lodash,因此可以使用它。

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"
}

Use object.entries to get the keys and values. 使用object.entries获取键和值。 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) 

You could use the new Object.fromEntries along with Object.entries: 您可以将新的Object.fromEntries和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' } 结果: { 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' } 结果: { a: 'apple', b: 'banana', c: 'carrot', d: 'diakon' }

 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); 

Another approach that avoinds the need for Object.fromEntries() , would be to use Array.reduce() as shown: 避免需要Object.fromEntries()另一种方法是使用Array.reduce() ,如下所示:

 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); 

If you've already got lodash, _.mapKeys is what you're looking for. 如果您已经有了lodash,则_.mapKeys是您想要的。 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: 这是您直接要求的内容(仅切成19个字符)的示例,但是您可以轻松地进行拆分或替换,或者执行其他操作:

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 这是一个Runkit: https ://runkit.com/daedalus28/slice-keys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM