fis*_*en0 6 javascript each jquery object
我无法this从以下javascript对象中删除(特定的"事件"),当this来自jquery .each()循环时.
weatherData:
{
"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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是.each()循环的缩小版本:
$.each(weatherData.events, function(i){
if(this.type == "One Time"){
delete weatherData.events[this];
}
})
Run Code Online (Sandbox Code Playgroud)
您正在使用一个需要字符串(属性名称)的对象.我相信你想:
$.each(weatherData.events, function(i){
if(this.type == "One Time"){
delete weatherData.events[i];
// change is here --------^
}
});
Run Code Online (Sandbox Code Playgroud)
...因为$.each将传递属性名称(例如"Move Out Day")作为迭代器函数的第一个参数,您接受它i.因此,要从对象中删除该属性,请使用该名称.