gpt4 book ai didi

javascript - delete object[element] 不会删除,而是将其设置为 null ....如何删除它而不是使其为 null?

转载 作者:行者123 更新时间:2023-12-01 00:03:26 32 4
gpt4 key购买 nike

Object.keys(tours).forEach(el => {
if (tours[el].id == 0) ***delete tours[el];***

});

我想删除 Tours 对象中的 Object,但此功能将其设置为 null,而不是删除它。

删除操作前的游览是这样的

[
{
"id": 0,
"name": "The Forest Hiker",
"duration": 5,
"maxGroupSize": 25,
"difficulty": "easy",
"ratingsAverage": 4.7
},
{
"id": 1,
"name": "The Sea Explorer",
"duration": 7,
"maxGroupSize": 15,
"difficulty": "medium",
"ratingsAverage": 4.8
}
]

删除后变成

[
null,

{
"id": 1,
"name": "The Sea Explorer",
"duration": 7,
"maxGroupSize": 15,
"difficulty": "medium",
"ratingsAverage": 4.8,
"ratingsQuantity": 23,
"price": 497,
"summary": "Exploring the jaw-dropping US east coast by foot and by boat",
"description": "Cpariatur.",
"imageCover": "tour-2-cover.jpg",
"images": ["tour-2-1.jpg", "tour-2-2.jpg", "tour-2-3.jpg"],
"startDates": ["2021-06-19,10:00", "2021-07-20,10:00", "2021-08-18,10:00"]
}
]

最佳答案

据我所知,tours 只是对象数组。所以你可以这样做:

根据任何条件删除

tours = tours.filter(t => t.id !== 0);

删除给定索引n

tours.splice(n, 1)

专门删除索引0

tours.shift()

关于javascript - delete object[element] 不会删除,而是将其设置为 null ....如何删除它而不是使其为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60559830/

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