jQuery 如何使用jQuery从数组中删除特定值

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

How to remove specific value from array using jQuery

jqueryarrays

提问by Elankeeran

I have an array that looks like this: var y = [1, 2, 3];

我有一个看起来像这样的数组: var y = [1, 2, 3];

I would like to remove 2from array y.

我想2从 array 中删除y

How can I remove a particular value from an array using jQuery? I have tried pop()but that always removes the last element.

如何使用 jQuery 从数组中删除特定值?我试过,pop()但总是删除最后一个元素。

回答by Sarfraz

A working JSFIDDLE

一个有效的JSFIDDLE

You can do something like this:

你可以这样做:

var y = [1, 2, 2, 3, 2]
var removeItem = 2;

y = jQuery.grep(y, function(value) {
  return value != removeItem;
});

Result:

结果:

[1, 3]

http://snipplr.com/view/14381/remove-item-from-array-with-jquery/

http://snipplr.com/view/14381/remove-item-from-array-with-jquery/

回答by user113716

With jQuery, you can do a single-line operation like this:

使用 jQuery,您可以执行这样的单行操作:

Example:http://jsfiddle.net/HWKQY/

示例:http : //jsfiddle.net/HWKQY/

y.splice( $.inArray(removeItem, y), 1 );

Uses the native .splice()and jQuery's $.inArray().

使用本机.splice()和 jQuery 的$.inArray().

回答by ddagsan

jQuery.filtermethod is useful. This is available for Arrayobjects.

jQuery.filter方法很有用。这可用于Array对象。

var arr = [1, 2, 3, 4, 5, 5];

var result = arr.filter(function(elem){
   return elem != 5; 
});//result -> [1,2,3,4]

http://jsfiddle.net/emrefatih47/ar0dhvhw/

http://jsfiddle.net/emrefatih47/ar0dhvhw/

In Ecmascript 6:

在 Ecmascript 6 中:

let values = [1,2,3,4,5];
let evens = values.filter(v => v % 2 == 0);
alert(evens);

https://jsfiddle.net/emrefatih47/nnn3c2fo/

https://jsfiddle.net/emrefatih47/nnn3c2fo/

回答by vatsal

You can use underscore.js. It really makes things simple.

您可以使用underscore.js。它真的让事情变得简单。

In your case all the code that you will have to write is -

在您的情况下,您必须编写的所有代码是 -

_.without([1,2,3], 2);

and the result will be [1,3].

结果将是 [1,3]。

It reduces the code that you write.

它减少了您编写的代码。

回答by Aamir Afridi

Not a jQuery way but... Why don't use simpler way. Remove 'c' from following array

不是 jQuery 方式,但是...为什么不使用更简单的方式。从以下数组中删除 'c'

var a = ['a','b','c','d']
a.splice(a.indexOf('c'),1);
>["c"]
a
["a", "b", "d"]

You can also use: (Note to myself: Don't modify objects you don't own)

您也可以使用:(请注意:不要修改您不拥有的对象

Array.prototype.remove = function(v) { this.splice(this.indexOf(v) == -1 ? this.length : this.indexOf(v), 1); }
var a = ['a','b','c'];
a.remove('c'); //value of "a" is now ['a','b']

Adding is simplera.push('c')

添加更简单a.push('c')

回答by praveen d

Remove Item in Array

删除数组中的项目

var arr = ["jQuery", "JavaScript", "HTML", "Ajax", "Css"];
var itemtoRemove = "HTML";
arr.splice($.inArray(itemtoRemove, arr), 1);

回答by yesnik

//This prototype function allows you to remove even array from array
Array.prototype.remove = function(x) { 
    var i;
    for(i in this){
        if(this[i].toString() == x.toString()){
            this.splice(i,1)
        }
    }
}

Example of using

使用示例

var arr = [1,2,[1,1], 'abc'];
arr.remove([1,1]);
console.log(arr) //[1, 2, 'abc']

var arr = [1,2,[1,1], 'abc'];
arr.remove(1);
console.log(arr) //[2, [1,1], 'abc']

var arr = [1,2,[1,1], 'abc'];
arr.remove('abc');
console.log(arr) //[1, 2, [1,1]]

To use this prototype function you need to paste it in your code. Then you can apply it to any array with 'dot notation':

要使用此原型函数,您需要将其粘贴到您的代码中。然后你可以将它应用到任何带有“点符号”的数组:

someArr.remove('elem1')

回答by AussieNick

There is no native way to do this in Javascript. You could use a library or write a small function to do this instead: http://ejohn.org/blog/javascript-array-remove/

在 Javascript 中没有本地方法可以做到这一点。您可以使用库或编写一个小函数来执行此操作:http: //ejohn.org/blog/javascript-array-remove/

回答by andrewb

My version of user113716's answer. His removes a value if no match is found, which is not good.

我的 user113716 版本的答案。如果没有找到匹配项,他会删除一个值,这是不好的。

var y = [1, 2, 3]
var removeItem = 2;

var i = $.inArray(removeItem,y)

if (i >= 0){
    y.splice(i, 1);
}

alert(y);

This now removes 1 item if a match is found, 0 if no matches are found.

现在,如果找到匹配项,则删除 1 个项目,如果未找到匹配项,则删除 0 个项目。

How it works:

这个怎么运作:

  • $.inArray(value, array) is a jQuery function which finds the first index of a valuein an array
  • The above returns -1 if the value is not found, so check that i is a valid index before we do the removal. Removing index -1 means removing the last, which isn't helpful here.
  • .splice(index, count) removes countnumber of values starting at index, so we just want a countof 1
  • $ .inArray(值,数组)是一个jQuery函数找到的第一个索引value中的array
  • 如果未找到该值,则上述返回 -1,因此在删除之前检查 i 是否为有效索引。删除索引 -1 意味着删除最后一个,这在这里没有帮助。
  • .splice(指数计数)删除count开始值的数量index,所以我们只是想count1

回答by Vahid Khakestani

You can use .not function like this:

您可以像这样使用 .not 函数:

var arr = [ 1 , 2 , 3 , 5 , 8];
var searchValue = 2;

var newArr = $(arr).not([searchValue]).get();