gpt4 book ai didi

javascript - 从 jQuery .each() 中的 javascript 对象中删除它

转载 作者:数据小太阳 更新时间:2023-10-29 05:13:18 24 4
gpt4 key购买 nike

我在删除 this 时遇到问题(特定的“事件”)来自以下 javascript 对象,当 this 时来自 jquery .each()循环。

天气数据:

{
"events":{
"Birthday":{
"type":"Annual",
"date":"20120523",
"weatherType":"clouds",
"high":"40",
"low":"30",
"speed":"15",
"direction":"0",
"humidity":"0"
},
"Move Out Day":{
"type":"One Time",
"date":"20120601",
"weatherType":"storm",
"high":"80",
"low":"76",
"speed":"15",
"direction":"56",
"humidity":"100"
}
},
"dates":{
"default":{
"type":"clouds",
"high":"40",
"low":"30",
"speed":"15",
"direction":"0",
"humidity":"0"
},
"20120521":{
"type":"clear",
"high":"60",
"low":"55",
"speed":"10",
"direction":"56",
"humidity":"25"
}
}
}

这是 .each() 的缩小版循环:

$.each(weatherData.events, function(i){
if(this.type == "One Time"){
delete weatherData.events[this];
}
})

最佳答案

您正在使用一个需要字符串(属性名称)的对象。我相信你想要:

$.each(weatherData.events, function(i){
if(this.type == "One Time"){
delete weatherData.events[i];
// change is here --------^
}
});

...因为 $.each 会将属性名称(例如,"Move Out Day")作为第一个参数传递给迭代器函数,您're accepting as i.因此,要从对象中删除该属性,您可以使用该名称。

Gratuitous live example | source

关于javascript - 从 jQuery .each() 中的 javascript 对象中删除它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10680892/

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