jquery javascript从JSON对象中删除对象数据 [英] jquery javascript remove object data from JSON object

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

问题描述

我的JSON对象看起来像下面的对象,这个对象可以持续数天。所以我想知道无论如何我可以删除全套,相当于下面例子中的位置[0]或位置[1]。我必须首先遍历Object并尝试找出哪个是哪个。所以我想说我要删除一个邮政编码为06238的集合,我需要遍历整个位置对象并找出它在对象中的哪个集合,然后相应地删除它。问题是我不确定如何处理这个概念。

I have JSON Object that looks something like the below object, this object can go on for days. So I am wondering is there anyway I can delete full set a set being the equivalent of locations[0] or locations[1] in the example below. I'd have to first iterate over the Object and try to figure out which one is which though. So lets say I am looking to remove an set where the zipcode is 06238, I would need to run over the entire locations object and find out which set it is in the object, then remove it accordingly. Question is I'm not sure how to approach that notion.

{
"locations": [
            {
                "city": "San Jose",
                "state": "CA",
                "zipcode": "95125",
                "longitude": "0",
                "latitude": "0"
            },
            {
                "city": "Coventry",
                "state": "CT",
                "zipcode": "06238",
                "longitude": "0",
                "latitude": "0"
            }
        ]
    }


推荐答案

只需传递索引

delete locations[0];

你经历了这样的正常JSON迭代

You go through a normal JSON iteration like this

jQuery.each(locations, function(i, val) {
   if(val.zipcode == "yourvalue") // delete index
   {
      delete locations[i];
  }
});

类似的东西。你在寻找一个想法或一个完整的功能吗?

something like that. are you looking for an idea or a complete function.

这是 jsFiddle

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

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