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

前端之家收集整理的这篇文章主要介绍了从jQuery .each()中的javascript对象中删除它前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我从以下 javascript对象中删除此(特定的“事件”)时遇到问题,这是来自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","direction":"56","humidity":"100"
        }
    },"dates":{
        "default":{
            "type":"clouds","20120521":{
            "type":"clear","high":"60","low":"55","speed":"10","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”)作为迭代器函数的第一个参数,您接受为i.因此,要从对象中删除属性,请使用该名称.

Gratuitous live example | source

原文链接:https://www.f2er.com/jquery/241596.html

猜你在找的jQuery相关文章