微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


jQuery表单验证提交:前台验证一(图文+视频)
jQuery表单验证提交:前台验证二(图文+视频)
jQuery如何实时监听获取input输入框的值
JQuery怎么判断元素是否存在
jQuery如何实现定时重定向
jquery如何获取复选框选中的值
jQuery如何清空form表单数据
jQuery怎么删除元素节点
JQuery怎么循环输出数组元素
jquery怎么实现点击刷新当前页面