gpt4 book ai didi

javascript - 通过键删除数组中的对象

转载 作者:行者123 更新时间:2023-11-28 16:55:07 24 4
gpt4 key购买 nike

我正在尝试通过 JavaScript 中的键删除对象

这是数组的示例

{
Account Manager: {selected: true}
Arrival: {selected: true}
Client: {selected: true}
Client Contact: {selected: true}
Created: {selected: true}
Created by: {selected: true}
Departure: {selected: true}
Destination: {selected: true}
Keywords: {selected: true}
Status: {selected: true}
}

现在我正在尝试从该数组中删除状态和客户端,但我不知道如何实现。我已经尝试过这个:

for(var i=0; i<list.length; i++) {
if(list[i] == 'Status' || list[i] == 'Client') {
list.splice(i, 1);
}
}

最佳答案

提供的示例是一个对象,而不是数组。由于您使用的是 AngularJS,因此您可以直接使用 JavaScript 从对象中删除 key 。

下面是简单使用delete()方法的示例

 const _object = {
"Account Manager": { selected: true },
"Arrival": { selected: true },
"Client": { selected: true },
"Client Contact": { selected: true },
"Created": { selected: true },
"Created by": { selected: true },
"Departure": { selected: true },
"Destination": { selected: true },
"Keywords": { selected: true },
"Status": { selected: true }
}

delete _object["Status"];

console.log(_object);

关于javascript - 通过键删除数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59353763/

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