最终数组应该有 uniqueIds
const characters = [
{
name: "Luke Skywalker",
height: "172",
mass: "77",
eye_color: "blue",
gender: "male",
},
{
name: "Darth Vader",
height: "202",
mass: "136",
eye_color: "yellow",
gender: "male",
},
{
name: "Leia Organa",
height: "150",
mass: "49",
eye_color: "brown",
gender: "female",
},
{
name: "Anakin Skywalker",
height: "188",
mass: "84",
eye_color: "blue",
gender: "male",
},
];
att = ["B0117", "B5019", "B0821", "B0098"\];
attArr = characters.map((tim) => {
return { ...tim, UniqueId: "B0117" }
});
console.log(attArr);
我想将数组(att)中的元素作为 uniqueID 添加到对象数组中
name: 'Anakin Skywalker',
height: '188',
mass: '84',
eye_color: 'blue',
gender: 'male',
UniqueId: 'B0117'
所有四个元素都应映射到 UniqueID uniqueId 应该具有数组中的值。
我想将其作为键值对插入[(https://www.stackoverflow.com/)
att = ["B0117", "B5019", "B0821", "B0098"];
attArr = characters.map((tim) => {
return { ...tim, UniqueId: "B0117" }
});
console.log(attArr);
你的方向是正确的,你只需要将索引传递给
att
arr i。 att[index]
const attArr = characters.map((character, index) => {
return { ...character, UniqueId: att[index] };
});
console.log(attArr);
代码将按以下方式分配唯一 ID: