如何重命名 javascript 中的多维数组键?
How to rename multidimensional array keys in javascript?
我正在尝试重命名此数组的键:
'oldname': [
{"name": "cat", "category": "animal"}
,{"name": "dog", "category": "animal"}
,{"name": "pig", "category": "animal"}
],
'oldname1': [
{"name": "pikachu", "category": "pokemon"}
,{"name": "Arbok", "category": "pokemon"}
,{"name": "Eevee", "category": "pokemon"}
]
我试过这个代码:
const obj = {oldKey: Object.values(result)};
delete Object.assign(results, {newname: obj.oldKey})['oldname'];
console.log(result);
但我做错了,因为我们不需要影响价值,它应该是这样的:
'newname': [
{"name": "cat", "category": "animal"}
,{"name": "dog", "category": "animal"}
,{"name": "pig", "category": "animal"}
],
'newname1': [
{"name": "pikachu", "category": "pokemon"}
,{"name": "Arbok", "category": "pokemon"}
,{"name": "Eevee", "category": "pokemon"}
]
使用 javascript 有更好的方法吗?
您可以使用 Object.entries
和 Object.fromEntries
来做到这一点
const replaceKeys = (data, mapping) =>
Object.fromEntries(
Object.entries(data).map(([k, v]) => [mapping[k] || k, v])
)
const data1 = {
oldName1: 1,
oldName2: 2,
anotherKey: 3
}
const mapping = {
oldName1: 'newName1',
oldName2: 'newName2'
}
console.log(replaceKeys(data1, mapping))
如果您不想创建新的 variable/array,您可以使用 for..of
循环和 Object.entries()
,如下所示:
const
input = { 'oldname': [ {"name": "cat", "category": "animal"} ,{"name": "dog", "category": "animal"} ,{"name": "pig", "category": "animal"} ], 'oldname1': [ {"name": "pikachu", "category": "pokemon"} ,{"name": "Arbok", "category": "pokemon"} ,{"name": "Eevee", "category": "pokemon"} ] },
mapping = {'oldname':'newname','oldname1':'newname1'};
for(let [key,value] of Object.entries(input)) {
input[mapping[key]] = value;
delete input[key];
}
console.log( input );
否则你可以按如下方式进行:
const
input = { 'oldname': [ {"name": "cat", "category": "animal"} ,{"name": "dog", "category": "animal"} ,{"name": "pig", "category": "animal"} ], 'oldname1': [ {"name": "pikachu", "category": "pokemon"} ,{"name": "Arbok", "category": "pokemon"} ,{"name": "Eevee", "category": "pokemon"} ] },
mapping = {'oldname':'newname','oldname1':'newname1','oldname2':'newname2','oldnamen':'newnamen'},
output = Object.fromEntries(
Object.entries(mapping).map(([oldname,newname]) => [newname, input[oldname]])
.filter(([,value]) => value)
);
console.log( input );
我正在尝试重命名此数组的键:
'oldname': [
{"name": "cat", "category": "animal"}
,{"name": "dog", "category": "animal"}
,{"name": "pig", "category": "animal"}
],
'oldname1': [
{"name": "pikachu", "category": "pokemon"}
,{"name": "Arbok", "category": "pokemon"}
,{"name": "Eevee", "category": "pokemon"}
]
我试过这个代码:
const obj = {oldKey: Object.values(result)};
delete Object.assign(results, {newname: obj.oldKey})['oldname'];
console.log(result);
但我做错了,因为我们不需要影响价值,它应该是这样的:
'newname': [
{"name": "cat", "category": "animal"}
,{"name": "dog", "category": "animal"}
,{"name": "pig", "category": "animal"}
],
'newname1': [
{"name": "pikachu", "category": "pokemon"}
,{"name": "Arbok", "category": "pokemon"}
,{"name": "Eevee", "category": "pokemon"}
]
使用 javascript 有更好的方法吗?
您可以使用 Object.entries
和 Object.fromEntries
来做到这一点
const replaceKeys = (data, mapping) =>
Object.fromEntries(
Object.entries(data).map(([k, v]) => [mapping[k] || k, v])
)
const data1 = {
oldName1: 1,
oldName2: 2,
anotherKey: 3
}
const mapping = {
oldName1: 'newName1',
oldName2: 'newName2'
}
console.log(replaceKeys(data1, mapping))
如果您不想创建新的 variable/array,您可以使用 for..of
循环和 Object.entries()
,如下所示:
const
input = { 'oldname': [ {"name": "cat", "category": "animal"} ,{"name": "dog", "category": "animal"} ,{"name": "pig", "category": "animal"} ], 'oldname1': [ {"name": "pikachu", "category": "pokemon"} ,{"name": "Arbok", "category": "pokemon"} ,{"name": "Eevee", "category": "pokemon"} ] },
mapping = {'oldname':'newname','oldname1':'newname1'};
for(let [key,value] of Object.entries(input)) {
input[mapping[key]] = value;
delete input[key];
}
console.log( input );
否则你可以按如下方式进行:
const
input = { 'oldname': [ {"name": "cat", "category": "animal"} ,{"name": "dog", "category": "animal"} ,{"name": "pig", "category": "animal"} ], 'oldname1': [ {"name": "pikachu", "category": "pokemon"} ,{"name": "Arbok", "category": "pokemon"} ,{"name": "Eevee", "category": "pokemon"} ] },
mapping = {'oldname':'newname','oldname1':'newname1','oldname2':'newname2','oldnamen':'newnamen'},
output = Object.fromEntries(
Object.entries(mapping).map(([oldname,newname]) => [newname, input[oldname]])
.filter(([,value]) => value)
);
console.log( input );