Javascript - 按值删除数组项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7142890/
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-24 01:01:25  来源:igfitidea点击:

Javascript - remove an array item by value

javascriptarrays

提问by itsme

My situation:

我的情况:

var id_tag = [1,2,3,78,5,6,7,8,47,34,90];

I would like to delete where id_tag = 90and to return:

我想delete where id_tag = 90并返回:

var id_tag = [1,2,3,78,5,6,7,8,47,34];

How can I do that?

我怎样才能做到这一点?

回答by Joseph Silber

You'll want to use JavaScript's Array splicemethod:

你会想要使用 JavaScript 的Arraysplice方法

var tag_story = [1,3,56,6,8,90],
    id_tag = 90,
    position = tag_story.indexOf(id_tag);

if ( ~position ) tag_story.splice(position, 1);

P.S.For an explanation of that cool ~tilde shortcut, see this post:

PS有关那个很酷的~波浪号快捷方式的解释,请参阅此帖子:

Using a ~tilde with indexOfto check for the existence of an item in an array.

使用~波浪号 withindexOf检查数组中是否存在某项



Note:IE < 9 does not support .indexOf()on arrays. If you want to make sure your code works in IE, you should use jQuery's $.inArray():

注意:IE < 9 不支持.indexOf()数组。如果你想确保你的代码在 IE 中工作,你应该使用 jQuery 的$.inArray()

var tag_story = [1,3,56,6,8,90],
    id_tag = 90,
    position = $.inArray(id_tag, tag_story);

if ( ~position ) tag_story.splice(position, 1);


If you want to support IE < 9 but don't already have jQuery on the page, there's no need to use it justfor $.inArray. You can use this polyfillinstead.

如果您想支持 IE < 9 但页面上还没有 jQuery,则无需将其用于$.inArray. 你可以改用这个 polyfill

回答by Pete

If you're going to be using this often (and on multiple arrays), extend the Array object to create an unset function.

如果您要经常使用它(并且在多个数组上),请扩展 Array 对象以创建一个未设置的函数。

Array.prototype.unset = function(value) {
    if(this.indexOf(value) != -1) { // Make sure the value exists
        this.splice(this.indexOf(value), 1);
    }   
}

tag_story.unset(56)

回答by Eli Grey

tag_story.splice(tag_story.indexOf(id_tag), 1);

回答by Peter Olson

function removeValue(arr, value) {
    for(var i = 0; i < arr.length; i++) {
        if(arr[i] === value) {
            arr.splice(i, 1);
            break;
        }
    }
    return arr;
}

This can be called like so:

这可以这样调用:

removeValue(tag_story, 90);

回答by disfated

As a variant

作为变种

delete array[array.indexOf(item)];

If you know nothing about deleteoperator, DON'T use this.

如果您对delete运算符一无所知,请不要使用此.

回答by Aleck Landgraf

I like to use filter:

我喜欢使用过滤器:

var id_tag = [1,2,3,78,5,6,7,8,47,34,90];

// delete where id_tag = 90
id_tag = id_tag.filter(function(x) {
    if (x !== 90) {
      return x;
    }
});

回答by yckart

Here are some helper functions I use:

以下是我使用的一些辅助函数:

Array.contains = function (arr, key) {
    for (var i = arr.length; i--;) {
        if (arr[i] === key) return true;
    }
    return false;
};

Array.add = function (arr, key, value) {
    for (var i = arr.length; i--;) {
        if (arr[i] === key) return arr[key] = value;
    }
    this.push(key);
};

Array.remove = function (arr, key) {
    for (var i = arr.length; i--;) {
        if (arr[i] === key) return arr.splice(i, 1);
    }
};

回答by J. Holmes

You'll want to use .indexOf()and .splice(). Something like:

你会想要使用.indexOf().splice()。就像是:

tag_story.splice(tag_story.indexOf(90),1);

回答by Gourav Sahare

You can use lodash.js

你可以使用 lodash.js

_.pull(arrayName,valueToBeRemove);

_.pull(arrayName,valueToBeRemove);

In your case :- _.pull(id_tag,90);

在你的情况下:- _.pull(id_tag,90);

回答by Siddhartha

var id_tag = [1,2,3,78,5,6,7,8,47,34,90]; 
var delete_where_id_tag = 90
    id_tag =id_tag.filter((x)=> x!=delete_where_id_tag);