Javascript 使用 Object.keys ES6 更改对象键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43839400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change object key using Object.keys ES6
提问by Alan Jenshen
I have
我有
var tab = {
abc:1,
def:40,
xyz: 50
}
I want to change the name of abc,def, xyz to something else, is it possible?
我想把 abc,def,xyz 的名字改成别的名字,可以吗?
I tried
我试过
const test = Object.keys(tab).map(key => {
if (key === 'abc') {
return [
a_b_c: tab[key]
]
}
});
console.log(test);
I got many undefined keys.
我有很多未定义的键。
采纳答案by Ravindra Ranwala
Here's how I solved it. I used a map to map between existing key and new key. Just substitute the map with whatever new values you need. Finally remove old keys from the object using omit.
这是我解决它的方法。我使用地图在现有密钥和新密钥之间进行映射。只需用您需要的任何新值替换地图。最后使用omit.
var tab = {
abc:1,
def:40,
xyz: 50
}
var map = {
abc : "newabc",
def : "newdef",
xyz : "newxyz"
}
_.each(tab, function(value, key) {
key = map[key] || key;
tab[key] = value;
});
console.log(_.omit(tab, Object.keys(map)));
回答by Samuli Hakoniemi
Here is the full code for replacing keys based on object that maps the values to replace:
以下是基于映射要替换的值的对象替换键的完整代码:
const tab = {abc: 1, def: 40, xyz: 50};
const replacements = {'abc': 'a_b_c', 'def': 'd_e_f'};
let replacedItems = Object.keys(tab).map((key) => {
const newKey = replacements[key] || key;
return { [newKey] : tab[key] };
});
This will output an array with three objects where keys are replaced. If you want to create a new object out of them, just:
这将输出一个包含三个对象的数组,其中的键被替换。如果你想用它们创建一个新对象,只需:
const newTab = replacedItems.reduce((a, b) => Object.assign({}, a, b));
This outputs: {"a_b_c": 1, "d_e_f": 40, "xyz": 50}
这输出: {"a_b_c": 1, "d_e_f": 40, "xyz": 50}
回答by Mohan Dere
With lodash mapKeysfunction its quite easy to transform object keys.
使用 lodash mapKeys函数可以很容易地转换对象键。
let tab = {
abc: 1,
def: 40,
xyz: 50
}
const map = {
abc: "newabc",
def: "newdef",
xyz: "newxyz"
}
// Change keys
_.mapKeys(tab, (value, key) => {
return map[value];
});
// -> { newabc: 1, newdef: 40, newxyz: 50 }
回答by Jeff Lowery
Shortest way I've found so far:
到目前为止我发现的最短方法:
const tab = {abc: 1, def: 40, xyz: 50};
const {'abc': 'a_b_c', 'def': 'd_e_f', ...rest} = tab;
tab = {'a_b_c', 'd_e_f', ...rest}
回答by Clemens
Here is a way to do it with deconstructing assignment and arrow functions.
这是一种使用解构赋值和箭头函数的方法。
const rename = (({abc: a_b_c, ...rest}) => ({a_b_c, ...rest}))
console.log(rename({abc: 1, def: 2}))
// { "a_b_c": 1, "def": 2 }
回答by Man Wai Lorentz Yip
That's easy with lodash.
使用 lodash 这很容易。
import { mapKeys } from 'lodash';
const tab = {
abc: 1,
def: 40,
xyz: 50
};
const test = mapKeys(tab, (value, key) => {
if (key === 'abc') return 'a_b_c';
return key;
});
回答by Shubham Khatri
You can add the new key and delete the old one.
您可以添加新密钥并删除旧密钥。
var tab = {
abc:1,
def:40,
xyz: 50
}
var key = 'abc'
console.log(key)
tab['a_b_c'] = tab[key]
delete tab[key]
console.log(tab);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
回答by Roshane Perera
hope this will help
希望这会有所帮助
initial data:
初始数据:
let tab = {
abc: 1,
def: 40,
xyz: 50
};
new key mappings:
新的键映射:
let newKeyMappings = {
abc: 'cab',
def: 'fed',
xyz: 'zyx'
};
mapping values with new keys
用新键映射值
let mapped = Object.keys(tab).map(oldKey=> {
let newKey = newKeyMappings[oldKey];
let result ={};
result[newKey]=tab[oldKey];
return result;
});
since mapped contains array of mapped object apply reduce operator
由于映射包含映射对象的数组应用减少运算符
let result = mapped.reduce((result, item)=> {
let key = Object.keys(item)[0];
result[key] = item[key];
return result;
}, {});

