我还在学习js,我需要一个建议。我从后端获取json对象,但是键有下划线。将关键字重命名为什么是最佳实践,例如将folder_name重命名为文件夹名称?属性列表是已知的,并且已完成,因此我可以将新名称保存在常量中。在前端,我已经这样使用它了:
const showPropertiesList = properties => Object.keys(properties).map(property => (
<PropertyKey key={property}}>
`${property}: ${properties[property]}`
</PropertyKey>
));最好在此映射中使用重命名函数或在此之前创建单独的函数,以获得所有重命名的键和值?
json文件:
properties {
folder_name: 'test',
user_email: 'test@example.com',
user_agreed: 1,
site: 'example.com',
}发布于 2018-03-05 09:40:15
您可以创建某种mapping对象,然后使用以下Object.keys和reduce函数的组合:
const properties = {
folder_name: "test",
user_email: "test@example.com",
user_agreed: 1,
site: "example.com"
};
const mapping = {
folder_name: "Folder name",
user_email: "User email",
user_agreed: "User agreed",
site: "Site"
};
const mapped = Object.keys(properties).reduce((acc, key) => {
acc[mapping[key]] = properties[key];
return acc;
}, {});
console.log(mapped);
发布于 2020-06-30 22:07:56
试试这个:
const { folder_name: folderName, user_email: email, ...others } = originObject;
const renamedPropsObject = { folderName, email, ...others }发布于 2018-03-05 09:49:02
我在旅行,所以我不能给自动取款机编程。但我认为这会把你引向正确的方向。
让newArray =数组()
OldArray.forEach(function(value, key) {
// do stuff here to change the key value
Let newKeyValue = //something
newArray[ newKeyValue] = value;
});
// do stuff with newArray希望能有所帮助。而不是测试它!
https://stackoverflow.com/questions/49102256
复制相似问题