简体   繁体   English

从JavaScript数组中删除元素

[英]Removing elements from JavaScript arrays

I have the following array setup, i,e: 我有以下数组设置,我,e:

var myArray = new Array();

Using this array, I am creating a breadcrumb menu dynamically as the user adds more menu items. 使用这个数组,我会在用户添加更多菜单项时动态创建面包屑菜单。 I am also allowing them to removing specific breadcrumb menu items by clicking on the cross alongside eatch breadcrumb menu item. 我还允许他们通过点击eatch breadcrumb菜单项旁边的十字架来删除特定的面包屑菜单项。

Array may hold the following data: 数组可能包含以下数据:

myArray[0] = 'MenuA';
myArray[1] = 'MenuB';
myArray[2] = 'MenuC';
myArray[3] = 'MenuD';
myArray[4] = 'MenuE';

My questions are: 我的问题是:

a) In JavaScript, how can I remove element [1] from myArray and then recalculate indexes or is this not possible? a)在JavaScript中,如何从myArray中删除元素[1]然后重新计算索引或这是不可能的?

b) If I don't want menu option MenuB, do I need to splice it to remove it? b)如果我不想要菜单选项MenuB,是否需要将其拼接以将其删除?

My problem is, if the user removes menu items as well as create news one at the end, how will the indexes to these elements be spreadout? 我的问题是,如果用户删除菜单项以及最后创建新闻,那么这些元素的索引将如何展开?

I just want to be able to remove items but don't know how the array indexes are handled. 我只是希望能够删除项目,但不知道如何处理数组索引。

You could use myArray.push('MenuA'); 你可以使用myArray.push('MenuA'); so you don't specify direct numbers when adding elements. 所以在添加元素时不要指定直接数字。

To remove an element IE 'MenuB': 删除元素IE'MenuB':

// another quick way to define an array
myArray = ['MenuA', 'MenuB', 'MenuC', 'MenuD', 'MenuE']; 

// remove an item by value:
myArray.splice(myArray.indexOf('MenuB'),1);

// push a new one on
myArray.push('MenuZ');

// myArray === ["MenuA", "MenuC", "MenuD", "MenuE", "MenuZ"]

I like this implementation of Array.remove, it basically abstracts the use of the splice function: 我喜欢Array.remove的这个实现 ,它基本上抽象了splice函数的使用:

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

Usage: 用法:

// Remove the second item from the array
array.remove(1);
// Remove the second-to-last item from the array
array.remove(-2);
// Remove the second and third items from the array
array.remove(1,2);
// Remove the last and second-to-last items from the array
array.remove(-2,-1);

http://www.w3schools.com/jsref/jsref_splice.asp http://www.w3schools.com/jsref/jsref_splice.asp

Splice should recalculate the correct indexes for future access. Splice应重新计算正确的索引以供将来访问。

You do not need to write a function, you can use indexOf() and splice() these two functions. 您不需要编写函数,可以使用indexOf()和splice()这两个函数。

You can remove any positional element a element. 您可以删除元素的任何位置元素。 For example: var name = ['james' , 'tommy' , 'Jimmy' , 'Holon']; 例如:var name = ['james','tommy','Jimmy','Holon']; var name = name.splice(name.indexOf('Jimmy') , 1); var name = name.splice(name.indexOf('Jimmy'),1);

Remove array element by position/element(Actual array modifies) 按位置/元素删除数组元素(实际数组修改)

1 - arr.splice(1, 1) -----> (index, no of elements) 1 - arr.splice(1,1)-----> (索引,没有元素)

2 - arr.splice(arr.indexOf(5), 1) -----> (array.indexOf( InputValue ), no of elements) 2 - arr.splice(arr.indexOf(5),1)-----> (array.indexOf( InputValue ),没有元素)

 let arr = [1,2,3,4,5]; console.log(arr.splice(1,1)); // [2] console.log(arr.splice(arr.indexOf(5), 1)); // [5] console.log(arr); // [1, 3, 4] 

Remove array element by position/element(creates copy array) 按位置/元素删除数组元素(创建副本数组)

 let arr2 = [10, 20, 30, 40] let result = arr2.filter(a=> a!==20); let result2 = arr2.filter(a=> a!==arr2[arr2.indexOf(30)]) console.log(result)   // [10, 30, 40] console.log(result2) // [10, 20, 40] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM