【发布时间】:2021-11-28 13:40:09
【问题描述】:
我需要更改对象的键。我可以使用 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 的内部对象列表中的键成为一个问题。修改内键的最佳方法是什么?
【问题讨论】:
-
你试过什么?尝试后会发生什么?
-
“内部”和“外部”数组之间没有区别。数组就是数组。