我有一个这样的对象数组:
let array = [{ firstName: "John", lastName : "Doe", id:5566, weight: 70 },{ firstName: "Francis", lastName : "Max", id:5567, weight: 85 }];
如何删除数组中所有对象的属性“ lastName”和“ weight”?
答案 0 :(得分:1)
array = array.map(person => ({ firstName: person.firstName, id: person.id }))
地图有些生锈,应该很近
答案 1 :(得分:1)
您可以将.map()
与对象分解和其他语法一起使用:
let data = [
{firstName: "John", lastName: "Doe", id:5566, weight: 70 },
{firstName: "Francis", lastName: "Max", id:5567, weight: 85 }
];
let result = data.map(({ lastName, weight, ...rest}) => rest);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
参考:
答案 2 :(得分:0)
尝试一下:
for(let i = 0; i < array.length; i++) {
array[i] = {
id: array[i].id,
firstName: array[i].firstName
}
}
基本上,您可以用仅包含所需属性的新对象替换数组中的每个对象。