使用jQuery通过值删除JSON数组内的元素 [英] Delete an element inside a JSON array by value with jQuery

查看:936
本文介绍了使用jQuery通过值删除JSON数组内的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的json数组的一部分

Part of my json Array

var videos = $j.parseJSON('
  [
    { "privacy":"public",
      "id":"1169341693" },

    { "privacy":"private",
      "id":"803641223" },

    { "privacy":"public",
      "id":"1300612600" }, ......

当我console.log我正在获取的元素时

When I console.log the element I'm getting

   [Object, Object, Object, …]
       0: Object
           privacy: "public"
           id: "1169341693"
       1: Object
           privacy: "private"
           id: "803641223"
       2: Object
           privacy: "public"
           id: "1300612600"

我还有一个要搜索的唯一ID

I also have a unique id I want to search for

var uniqueId = 803641223;

我想在我的视频数组中找到正确的ID,然后删除整个数组元素.因此,在那种情况下,我希望我的最终视频数组仅包含2个对象,而不是3个:

I want to find, in my videos array, the right id, and delete that whole array element. So In that case, I want my final videos array to contain only 2 object, instead of 3 :

 var videos = $j.parseJSON('
  [
    { "privacy":"public",
      "id":"1169341693" },

    { "privacy":"public",
      "id":"1300612600" }, ......

我的问题是如何进入阵列进行拼接.我更喜欢用jQuery

My problem is how to get in the array to do my splice. I prefer to do it with jQuery

请帮忙吗?

推荐答案

您可以使用 grep :

videos = $.grep(videos, function(e) { return e.id!='803641223' });

在普通JavaScript中,您可能已经使用了类似的 filter 函数但是IE8不支持.

In vanilla JavaScript you could have used the similar filter function but it's not supported by IE8.

请注意,videos是一个JavaScript数组,它不是JSON数组,即使它是通过解析JSON字符串制成的.

Please note that videos is a JavaScript array, it's not a JSON array, even if it was made by parsing a JSON string.

这篇关于使用jQuery通过值删除JSON数组内的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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