从jQuery .each()中的javascript对象中删除它 [英] Delete this from a javascript object in a jQuery .each()

查看:88
本文介绍了从jQuery .each()中的javascript对象中删除它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从以下javascript对象中删除(特定事件),当时来自jquery .each()循环。

I am having trouble deleting this (a specific 'event') from the following javascript object, when this is from a jquery .each() loop.

weatherData:

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"
        }
    }
}

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

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


推荐答案

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

You're using an object where a string (the property name) is expected. I believe you want:

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

...因为 $。每个将传递属性名称(例如,移出日期)作为迭代器函数的第一个参数,您接受为 i 。因此,要从对象中删除该属性,请使用该名称。

...because $.each will pass in the property name (e.g., "Move Out Day") as the first argument to the iterator function, which you're accepting as i. So to delete that property from the object, you use that name.

无偿的实例 | 来源

这篇关于从jQuery .each()中的javascript对象中删除它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆