Removing Elements from JavaScript Arrays

I have the following array settings, i, e:

var myArray = new Array(); 

Using this array, I dynamically create the breadcrumbs menu as the user adds additional menu items. I also allow them to delete certain menu items with breadcrumbs by clicking on the cross next to the eatch breadcrumb menu item.

An array may contain the following data:

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

My questions:

a) How to remove element [1] from myArray in JavaScript and then recalculate indexes or is it impossible?

b) If I do not need the MenuB menu option, do I need to splic it to remove it?

My problem is that if the user deletes the menu items and then creates the news at the end, how will the indexes to these items be distributed?

I just want to be able to delete elements, but I don’t know how the indexes of the array are processed.

+4
source share
5 answers

I like this implementation in Array.remove, it basically abstracts the use of 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); }; 

Using:

 // 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); 
+20
source

You can use myArray.push('MenuA'); to avoid specifying direct numbers when adding items.

To remove the IE element '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"] 
+30
source

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

The alloy must recount the correct indexes for future access.

+3
source

You do not need to write a function, you can use the indexOf () and splice () functions of these two functions.

You can remove any element of a positional element. For example: var name = ['james', 'tommy', 'Jimmy', 'Holon']; var name = name.splice (name.indexOf ('Jimmy'), 1);

0
source

Delete array element by position / element (Changes to the actual array)

1 - arr.splice (1, 1) - β†’ (index, without elements)

2 - arr.splice (arr.indexOf (5), 1) - β†’ (array.indexOf (InputValue), without elements)

 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] 

Delete an array element by position / element (creates a copy of the 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] 

0
source

All Articles