gpt4 book ai didi

javascript - 如何从对象数组中删除属性

转载 作者:行者123 更新时间:2023-12-04 07:55:02 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Remove property for all objects in array

(16 个回答)


5 个月前关闭。




我有一个带有 name 的对象数组和 age属性(property):

[
{ name: "Matthew", age: 23 },
{ name: "James", age: 20 },
{ name: "Daniel", age: 25 },
{ name: "Joshua", age: 22 }
]
我要删除 age来自所有对象的属性并在控制台中打印
[
{ name: "Matthew" },
{ name: "James" },
{ name: "Daniel" },
{ name: "Joshua" }
]

最佳答案

遍历您的数组并使用 delete关键词。

let array = [
{ name: "Matthew", age: 23 },
{ name: "James", age: 20 },
{ name: "Daniel", age: 25 },
{ name: "Joshua", age: 22 }
]
array.forEach(function(v){ delete v.age });
console.log(array);

关于javascript - 如何从对象数组中删除属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66740928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com