Javascript loop and remove from array

There are lot of wonderful answers on this thread already. However I wanted to share my experience when I tried to solve "remove nth element from array" in ES5 context.

JavaScript arrays have different methods to add/remove elements from start or end. These are:

arr.push[ele] - To add element[s] at the end of the array 
arr.unshift[ele] - To add element[s] at the beginning of the array
arr.pop[] - To remove last element from the array 
arr.shift[] - To remove first element from the array 

Essentially none of the above methods can be used directly to remove nth element from the array.

A fact worth noting is that this is in contrast with java iterator's using which it is possible to remove nth element for a collection while iterating.

This basically leaves us with only one array method Array.splice to perform removal of nth element [there are other things you could do with these methods as well, but in the context of this question I am focusing on removal of elements]:

Array.splice[index,1] - removes the element at the index 

Here is the code copied from original answer [with comments]:

var arr = ["one", "two", "three", "four"];
var i = arr.length; //initialize counter to array length 

while [i--] //decrement counter else it would run into IndexOutBounds exception
{
  if [arr[i] === "four" || arr[i] === "two"] {
    //splice modifies the original array
    arr.splice[i, 1]; //never runs into IndexOutBounds exception 
    console.log["Element removed. arr: "];

  } else {
    console.log["Element not removed. arr: "];
  }
  console.log[arr];
}

Another noteworthy method is Array.slice. However the return type of this method is the removed elements. Also this doesn't modify original array. Modified code snippet as follows:

var arr = ["one", "two", "three", "four"];
var i = arr.length; //initialize counter to array length 

while [i--] //decrement counter 
{
  if [arr[i] === "four" || arr[i] === "two"] {
    console.log["Element removed. arr: "];
    console.log[arr.slice[i, i + 1]];
    console.log["Original array: "];
    console.log[arr];
  }
}

Having said that, we can still use Array.slice to remove nth element as shown below. However it is lot more code [hence inefficient]

var arr = ["one", "two", "three", "four"];
var i = arr.length; //initialize counter to array length 

while [i--] //decrement counter 
{
  if [arr[i] === "four" || arr[i] === "two"] {
    console.log["Array after removal of ith element: "];
    arr = arr.slice[0, i].concat[arr.slice[i + 1]];
    console.log[arr];
  }

}

The Array.slice method is extremely important to achieve immutability in functional programming à la redux

In this tutorial, you will find an elegant way of looping through an Array and removing items without breaking the for loop.

The splice[] method is used to remove an element; however, the array is being re-indexed when you run splice[], which means that you will skip over an index when one is removed. To fix it, you can either decrement i after a splice[] or simply iterate in reverse:

Javascript splice method array remove items without breaking the for loop

let elements = [1, 3, 3, 5, 3, 1, 4]; for [let i = 0; i < elements.length; i++] { if [elements[i] == 3] { elements.splice[i, 1]; } } console.log[elements];

Javascript splice method array remove items without breaking the for loop

let elements = [1, 3, 3, 5, 3, 1, 4]; let removeEl = 3; let index = elements.indexOf[removeEl]; while [index !== -1] { elements.splice[index, 1]; index = elements.indexOf[removeEl]; } console.log[elements];

Javascript splice method array remove items without breaking the for loop

let elements = [1, 3, 8, 5, 16, 1, 4]; for [i = elements.length - 1; i >= 0; --i] { if [elements[i] % 2 === 0] { elements.splice[i, 1]; // Remove even numbers } } console.log[elements];

This will prevent the re-index from affecting the next item in the iteration because indexing has an effect only on the items from the current point to the end of an Array, and in the iteration, the next item is lower than the current point.

The splice[] method modifies the contents of an Array by removing or replacing the existing elements or adding new elements in place. While it changes the original array in-place, it still returns the list of removed items. If there is no removed Array, it returns an empty one.

How do you delete an element from an array loop?

Use unset[] function to remove array elements in a foreach loop. The unset[] function is an inbuilt function in PHP which is used to unset a specified variable.

How do you remove an element from an array in JavaScript?

Array elements can be deleted using the JavaScript operator delete . Using delete leaves undefined holes in the array. Use pop[] or shift[] instead.

How do I remove an object from an array of objects?

To remove an object from an array in Javascript,.
pop[] – The pop[] method removes from the end of an Array..
splice[] – The splice[] method deletes from a specific Array index..
shift[] – The shift[] method removes from the beginning of an Array..

Can we remove any element by using it for each loop?

Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove .

Chủ Đề