使用 JavaScript 更改数组中嵌套对象的键
Changing the keys of a nested object in an Array with JavaScript
我需要更改对象的键。我可以使用 map 函数来更改外部对象的键。问题是,我怎样才能访问数组中的内部对象。在下面的代码中,我需要将 team
键更改为 teamName
。我的结构必须按照相同的顺序。
let myArray = [
{
id: 1,
name: "foo",
Organization: [{ team: "value1" }, { location: "value2" }],
},
{
id: 2,
name: "foo",
Organization: [{ team: "value1" }, { location: "value2" }],
},
];
如果我想将 id
更改为 userId
,我可以像这样更改外部数组的键。
const newArray = myArray.map((item) => {
return {
userId: item.id,
};
});
但是尝试更改 Organization
的内部对象列表中的键成为一个问题。修改内键的最佳方法是什么?
选项 1 - lodash mapKeys
import { mapKeys } from 'lodash';
const newArray = myArray.map(item => ({
...item,
Organization: item.Organization.map(org =>
mapKeys(org, (_, key) => (key === 'team' ? 'teamName' : key))
),
}));
选项 2 - 对象销毁
只要 team
存在,您就可以销毁每个 Organization
并用 teamName
重建它。
const newArray = myArray.map(item => ({
...item,
Organization: item.Organization.map(({ team, ...rest }) =>
Object.assign(rest, team ? { teamName: team } : {})
),
}));
结果
[
{
id: 1,
name: 'foo',
Organization: [{ teamName: 'value1' }, { location: 'value2' }],
},
{
id: 2,
name: 'foo',
Organization: [{ teamName: 'value1' }, { location: 'value2' }],
},
];
如果Organization
总是一个有2个元素的数组。其中第一个元素是 属性 team
的对象,第二个元素是 属性 location
的对象。然后下面的代码就可以了。
let myArray = [{
"id": 1,
"name": "foo",
"Organization": [{"team": "value1"}, {"location": "value2"}]
}, {
"id": 2,
"name": "foo",
"Organization": [{"team": "value1"}, {"location": "value2"}]
}];
const result = myArray.map((item) => {
const [{ team: teamName }, location] = item.Organization;
return { ...item, Organization: [{ teamName }, location] };
});
console.log(result);
这个答案使用了destructuring assignment。如果您不知道这是什么,我强烈建议您查看链接的文档。
再简单不过了。
console.log(
[{
"id": 1,
"name": "foo",
"Organization": [{
"team": "value1"
}, {
"location": "value2"
}]
},
{
"id": 2,
"name": "foo",
"Organization": [{
"team": "value1"
}, {
"location": "value2"
}]
},
].reduce((a, b) => {
b.Organization[0] = {
teamName: b.Organization[0].team
}
a.push(b)
return a
}, [])
)
我需要更改对象的键。我可以使用 map 函数来更改外部对象的键。问题是,我怎样才能访问数组中的内部对象。在下面的代码中,我需要将 team
键更改为 teamName
。我的结构必须按照相同的顺序。
let myArray = [
{
id: 1,
name: "foo",
Organization: [{ team: "value1" }, { location: "value2" }],
},
{
id: 2,
name: "foo",
Organization: [{ team: "value1" }, { location: "value2" }],
},
];
如果我想将 id
更改为 userId
,我可以像这样更改外部数组的键。
const newArray = myArray.map((item) => {
return {
userId: item.id,
};
});
但是尝试更改 Organization
的内部对象列表中的键成为一个问题。修改内键的最佳方法是什么?
选项 1 - lodash mapKeys
import { mapKeys } from 'lodash';
const newArray = myArray.map(item => ({
...item,
Organization: item.Organization.map(org =>
mapKeys(org, (_, key) => (key === 'team' ? 'teamName' : key))
),
}));
选项 2 - 对象销毁
只要 team
存在,您就可以销毁每个 Organization
并用 teamName
重建它。
const newArray = myArray.map(item => ({
...item,
Organization: item.Organization.map(({ team, ...rest }) =>
Object.assign(rest, team ? { teamName: team } : {})
),
}));
结果
[
{
id: 1,
name: 'foo',
Organization: [{ teamName: 'value1' }, { location: 'value2' }],
},
{
id: 2,
name: 'foo',
Organization: [{ teamName: 'value1' }, { location: 'value2' }],
},
];
如果Organization
总是一个有2个元素的数组。其中第一个元素是 属性 team
的对象,第二个元素是 属性 location
的对象。然后下面的代码就可以了。
let myArray = [{
"id": 1,
"name": "foo",
"Organization": [{"team": "value1"}, {"location": "value2"}]
}, {
"id": 2,
"name": "foo",
"Organization": [{"team": "value1"}, {"location": "value2"}]
}];
const result = myArray.map((item) => {
const [{ team: teamName }, location] = item.Organization;
return { ...item, Organization: [{ teamName }, location] };
});
console.log(result);
这个答案使用了destructuring assignment。如果您不知道这是什么,我强烈建议您查看链接的文档。
再简单不过了。
console.log(
[{
"id": 1,
"name": "foo",
"Organization": [{
"team": "value1"
}, {
"location": "value2"
}]
},
{
"id": 2,
"name": "foo",
"Organization": [{
"team": "value1"
}, {
"location": "value2"
}]
},
].reduce((a, b) => {
b.Organization[0] = {
teamName: b.Organization[0].team
}
a.push(b)
return a
}, [])
)