使用jQuery按值删除JSON数组中的元素

时间:2022-10-26 10:36:51

Part of my json Array

我的json数组的一部分

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

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

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

When I console.log the element I'm getting

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

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

I also have a unique id I want to search for

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

var uniqueId = 803641223;

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 :

我想在我的视频数组中找到正确的id,并删除整个数组元素。那么在这种情况下,我希望我的最终视频数组只包含2个对象,而不是3个:

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

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

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

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

Any help please?

有什么帮助吗?

4 个解决方案

#1


13  

You can use grep :

你可以使用grep:

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

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

在vanilla JavaScript中,您可以使用类似的过滤功能,但IE8不支持它。

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

请注意,视频是一个JavaScript数组,它不是JSON数组,即使它是通过解析JSON字符串制作的。

#2


4  

A non-jQuery solution that modifies the array in place:

一个非jQuery解决方案,可以修改数组:

var uniqueId = 803641223;
var videos = [
    { "privacy":"public",
      "id":"1169341693" },

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

    { "privacy":"public",
      "id":"1300612600" }
];

function cleaner(arr, id) {
    for (var i = 0; i < videos.length; i++) {
        var cur = videos[i];
        if (cur.id == uniqueId) {
            arr.splice(i, 1);
            break;
        }
    }
}

cleaner(videos, uniqueId);

http://jsfiddle.net/4JAww/1/

http://jsfiddle.net/4JAww/1/

Note that this modifies the original array in place, such that the original videos array will have the items you want, and the one that matched the uniqueId will be gone (forever). So it depends on whether you want to be able to access the original array ever again, or are okay with modifying it.

请注意,这会修改原始数组,以便原始视频数组将包含您想要的项目,并且匹配uniqueId的数组将会消失(永远)。因此,这取决于您是否希望能够再次访问原始阵列,或者可以修改它。

It just loops through the elements of the array, compares the item's id property to the uniqueId value, and splices if they match. I use break; immediately after the splice because you seem to imply that the uniqueId can/should only appear once in the array since it's...unique.

它只是循环遍历数组的元素,将item的id属性与uniqueId值进行比较,并在它们匹配时进行拼接。我用休息;在拼接之后立即因为你似乎暗示uniqueId可以/应该只在数组中出现一次,因为它是...唯一的。

#3


3  

Hello you can remove element with javascript splice function...

你好你可以用javascript拼接功能删除元素...

videos.items.splice(1, 3); // Removes three items starting with the 2nd,

#4


0  

It worker for me.

它适合我。

  arrList = $.grep(arrList, function (e) { 

        if(e.add_task == addTask && e.worker_id == worker_id) {
            return false;
        } else {
            return true;
        }
    });

It returns an array without that object.

它返回一个没有该对象的数组。

Hope it helps.

希望能帮助到你。

#1


13  

You can use grep :

你可以使用grep:

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

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

在vanilla JavaScript中,您可以使用类似的过滤功能,但IE8不支持它。

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

请注意,视频是一个JavaScript数组,它不是JSON数组,即使它是通过解析JSON字符串制作的。

#2


4  

A non-jQuery solution that modifies the array in place:

一个非jQuery解决方案,可以修改数组:

var uniqueId = 803641223;
var videos = [
    { "privacy":"public",
      "id":"1169341693" },

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

    { "privacy":"public",
      "id":"1300612600" }
];

function cleaner(arr, id) {
    for (var i = 0; i < videos.length; i++) {
        var cur = videos[i];
        if (cur.id == uniqueId) {
            arr.splice(i, 1);
            break;
        }
    }
}

cleaner(videos, uniqueId);

http://jsfiddle.net/4JAww/1/

http://jsfiddle.net/4JAww/1/

Note that this modifies the original array in place, such that the original videos array will have the items you want, and the one that matched the uniqueId will be gone (forever). So it depends on whether you want to be able to access the original array ever again, or are okay with modifying it.

请注意,这会修改原始数组,以便原始视频数组将包含您想要的项目,并且匹配uniqueId的数组将会消失(永远)。因此,这取决于您是否希望能够再次访问原始阵列,或者可以修改它。

It just loops through the elements of the array, compares the item's id property to the uniqueId value, and splices if they match. I use break; immediately after the splice because you seem to imply that the uniqueId can/should only appear once in the array since it's...unique.

它只是循环遍历数组的元素,将item的id属性与uniqueId值进行比较,并在它们匹配时进行拼接。我用休息;在拼接之后立即因为你似乎暗示uniqueId可以/应该只在数组中出现一次,因为它是...唯一的。

#3


3  

Hello you can remove element with javascript splice function...

你好你可以用javascript拼接功能删除元素...

videos.items.splice(1, 3); // Removes three items starting with the 2nd,

#4


0  

It worker for me.

它适合我。

  arrList = $.grep(arrList, function (e) { 

        if(e.add_task == addTask && e.worker_id == worker_id) {
            return false;
        } else {
            return true;
        }
    });

It returns an array without that object.

它返回一个没有该对象的数组。

Hope it helps.

希望能帮助到你。