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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15367734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:54:51  来源:igfitidea点击:

Delete an element inside a JSON array by value with jQuery

javascriptjqueryarraysjson

提问by Lelly

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?

请问有什么帮助吗?

回答by Denys Séguret

You can use grep:

您可以使用grep

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

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

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

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

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

回答by Ian

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 videosarray will have the items you want, and the one that matched the uniqueIdwill 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.

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

It just loops through the elements of the array, compares the item's idproperty to the uniqueIdvalue, and splices if they match. I use break;immediately after the splicebecause you seem to imply that the uniqueIdcan/should only appear once in the array since it's...unique.

它只是遍历数组的元素,将项目的id属性与uniqueId值进行比较,splice如果匹配则为 s。我break;在 the 之后立即使用,splice因为您似乎暗示uniqueIdcan/should只在数组中出现一次,因为它是...独一无二的。

回答by Shail

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

您好,您可以使用 javascript splice 函数删除元素...

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

回答by Siva Anand

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.

希望能帮助到你。