jQuery 在jquery中按索引删除数组值

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

Remove Array Value By index in jquery

jquery

提问by Baskar

Array:

大批:

var arr = {'abc','def','ghi'};

I want to remove above array value 'def' by using index.

我想通过使用索引删除上面的数组值“def”。

回答by Kumaran

Use the splice method.

使用拼接方法

ArrayName.splice(indexValueOfArray,1);

This removes 1item from the array starting at indexValueOfArray.

1将从从 开始的数组中删除项目indexValueOfArray

回答by mekwall

Your example code is wrong and will throw a SyntaxError. You seem to have confused the syntax of creating an object Objectwith creating an Array.

您的示例代码是错误的,会引发 SyntaxError。您似乎将创建对象的语法Object与创建Array.

The correct syntax would be: var arr = [ "abc", "def", "ghi" ];

正确的语法是: var arr = [ "abc", "def", "ghi" ];

To remove an item from the array, based on its value, use the splicemethod:

要根据其值从数组中删除项目,请使用splice方法:

arr.splice(arr.indexOf("def"), 1);

To remove it by index, just refer directly to it:

要通过索引删除它,只需直接引用它:

arr.splice(1, 1);

回答by peterjwest

Your syntax is incorrect, you should either specify a hash:

您的语法不正确,您应该指定一个哈希值:

hash = {abc: true, def: true, ghi: true};

Or an array:

或者一个数组:

arr = ['abc','def','ghi'];

You can effectively remove an item from a hash by simply setting it to null:

您可以通过简单地将其设置为 null 来有效地从散​​列中删除项目:

hash['def'] = null;
hash.def = null;

Or removing it entirely:

或者完全删除它:

delete hash.def;

To remove an item from an array you have to iterate through each item and find the one you want (there may be duplicates). You could use array searching and splicing methods:

要从数组中删除项目,您必须遍历每个项目并找到您想要的项目(可能有重复项)。您可以使用数组搜索和拼接方法:

arr.splice(arr.indexOf("def"), 1);

This finds the first index of "def" and then removes it from the array with splice. However I would recommend .filter() because it gives you more control:

这将找到“def”的第一个索引,然后使用 splice 将其从数组中删除。但是我会推荐 .filter() 因为它给你更多的控制:

arr.filter(function(item) { return item !== 'def'; });

This will create a new array with only elements that are not 'def'.

这将创建一个仅包含非“def”元素的新数组。

It is important to note that arr.filter() will return a new array, while arr.splice will modify the original array and return the removed elements. These can both be useful, depending on what you want to do with the items.

需要注意的是 arr.filter() 会返回一个新数组,而 arr.splice 会修改原数组并返回移除的元素。这些都可能很有用,具体取决于您想对这些项目做什么。

回答by Anton

delete arr[1]

Try this out, it should work if you have an array like var arr =["","",""]

试试这个,如果你有一个像 var 这样的数组它应该可以工作 arr =["","",""]

回答by Dhanasekar

  1. Find the element in array and get its position
  2. Remove using the position
  1. 查找数组中的元素并获取其位置
  2. 使用位置删除

var array = new Array();
  
array.push('123');
array.push('456');
array.push('789');
                 
var _searchedIndex = $.inArray('456',array);
alert(_searchedIndex );
if(_searchedIndex >= 0){
  array.splice(_searchedIndex,1);
  alert(array );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

  • inArray() - helps you to find the position.
  • splice() - helps you to remove the element in that position.
  • inArray() - 帮助您找到位置。
  • splice() - 帮助您删除该位置的元素。