Remove item from array using slice

I'm trying to remove an element from my array using a slice, but I can't get it to work, look at this code snippet.

console.log(this.activeEffects); // Prints my array console.log(this.activeEffects.slice(0,1)); // Remove from index 0, and remove one. console.log(this.activeEffects); // Prints array again, this time my element should be gone 

The result of this.

enter image description here

So, what comes of this, first the array is intact, as it should be. Then it prints what is sliced ​​into an array. Finally, should the third be empty? or?

+12
source share
7 answers

I believe you are looking for splice . From W3 School:

The splice () method adds / removes elements to / from the array and returns deleted elements.

Take a look at the example on this page; The usage example is similar to what you want to achieve.

EDIT: Alternative reference to MDN as suggested by Nicosunshine; much more information about the team there.

+15
source

.slice does not mutate the array, you can use .splice() to remove the element at index i in the array:

 this.activeEffects.splice(i, 1) 
+7
source

Here is what I came up with:

 var newArray = oldArray.slice(indexOfElementToRemove+1).concat(oldArray.slice(0,indexOfElementToRemove)); 
+5
source
 function removeItemWithSlice(index) { return [...items.slice(0, index), ...items.slice(index + 1)] } 

Slice will create a new array. We create two arrays: from the beginning to the index and from index + 1 to the end. Then we use the spread operator (...) to take the elements of these arrays and create a new single array containing all the elements that we care about. I will insert the equivalent path if you do not like one liner:

 function removeItemWithSlice(index) { const firstArr = items.slice(0, index); const secondArr = items.slice(index + 1); return [...firstArr , ...secondArr] } 
+5
source
 a.slice(0, index).concat(a.slice(index + 1)) 
+2
source

Array.prototype. slice() Array.prototype. slice() ...

does not modify the original array, but returns a new "one level" deep "copy containing copies of the elements cut from the original array. Elements of the original array are copied to the new array as follows:

While Array.prototype. splice() Array.prototype. splice() ...

Modifies the contents of an array by adding new elements while deleting old elements.

This example should illustrate the difference.

 // sample array var list = ["a","b","c","d"]; // slice returns a new array console.log("copied items: %o", list.slice(2)); // but leaves list itself unchanged console.log("list: %o", list); // splice modifies the array and returns a list of the removed items console.log("removed items: %o", list.splice(2)); // list has changed console.log("list: %o", list); 
+1
source

Take a look here: http://www.w3schools.com/jsref/jsref_slice_array.asp

You can see that the slice method selects the et object and places them in a new array object ^^ So you cannot delete such an object, maybe you can try the following:

 var a = ["a","b","c"]; (pseudo code) /* I wan't to remove the "b" object */ var result = a.slice(0,1)+a.slice(2,1); /* If you considers that "+" is a concatenation operator, i don't remember if it is true... */ 
-1
source

Source: https://habr.com/ru/post/922292/


All Articles