const oldArr = [
{ label: 'a', value: 'b' },
{ label: 'c', value: 'd' },
{ label: 'e', value: 'f' },
];
let newArr = [];
oldArr.map((item, index) => {
newArr.push(Object.assign({}, item, { key: index }));
return newArr;
});
console.log(newArr)
// 结果
{ label: 'a', value: 'b', key: '0' },
{ label: 'c', value: 'd', key: '1' },
{ label: 'e', value: 'f', key: '2' },
遍历数组并给数组每项添加key值
最新推荐文章于 2024-05-11 11:32:11 发布