25

I am trying to remove a element from my array using slice, but i can't get it to work, look at this piece of code.

    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

Result of this is.

enter image description here

So what is get from this is, at first the array is whole, as it should be. Then its prints what is sliced of the array. Finally the third should be empty? or?

2
  • 1
    it is possible to make jsfiddle for this ? Commented Aug 7, 2012 at 14:50
  • Are you sure this is an array? Commented Aug 7, 2012 at 14:51

7 Answers 7

52
function removeItemWithSlice(index) {
  return [...items.slice(0, index), ...items.slice(index + 1)]
}

Slice will create a new array. We create two arrays: from beggining to index and from index+1 to end. Then we apply the spread operator (...) to take the items of those arrays and create a new single array containing all the items we care. I will paste an equivalent way if you don't like the one liner:

function removeItemWithSlice(index) {
  const firstArr = items.slice(0, index);
  const secondArr = items.slice(index + 1);
  return [...firstArr , ...secondArr]
}
Sign up to request clarification or add additional context in comments.

1 Comment

This should be the accepted answer since it solves the literal question. Splice and Slice do not behave the same when it comes to mutation.
30

I believe you're looking for splice. From W3 Schools:

The splice() method adds/removes items to/from an array, and returns the removed item(s).

Take a look at the example on that page; the use case there is similar to what you want to achieve.

EDIT: Alternative link to MDN, as suggested by Nicosunshine; much more information about the command there.

6 Comments

I know I might sound annoying, but don't use W3 Schools. MDN it's a much better resource developer.mozilla.org/en-US/docs/JavaScript/Reference/…
Thank you TSL, this solved my problem. Slapping my self right now.
@nicosunshine to Meta to request auto-block of w3schools links!
@canon I don't know if that would fly, but you can always spam w3fools.com in all stack overflow :P
The OP asked for slice. There's a reason to use slice if you wish not to mutate values.
|
13
a.slice(0, index).concat(a.slice(index + 1))

3 Comments

Instead of throwing a code snippet at OP, better to give some additional explanation, e.g. why their solution doesn't work, how yours does, and maybe link to additional documentation to the used functions in question.
@kingdaro My type of thinking adressed to a strong developer.
technically, this is the right answer - and yes, you might want to use splice instead :-)
8

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

this.activeEffects.splice(i, 1)

4 Comments

But i may need to remove a element i the middle of the array
@MartinElvarJensen right, I didn't realize that. shift() is preferable when you want to remove from the beginning.
slice does not change the original array as he says but it does return the removed elements. Why not then replace the original array with the removed elements by doing an assignment? e.g. this.activeEffects = this.activeEffects.slice(0,1)
@Magrangs you would need to reverse that. Something like: this.activeEffects = this.activeEffects.slice(1) It makes a new array that has all the items except for the first one. But if there are other references to the old array, they won't be updated.
5

This is what I was able to come up with :

var newArray = oldArray.slice(indexOfElementToRemove+1).concat(oldArray.slice(0,indexOfElementToRemove));

1 Comment

this changes the order of the array
1

Array.prototype.slice()...

does not alter the original array, but returns a new "one level deep" copy that contains copies of the elements sliced from the original array. Elements of the original array are copied into the new array as follows:

Whereas Array.prototype.splice()...

Changes the content of an array, adding new elements while removing 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);

Comments

-1

Look at here : http://www.w3schools.com/jsref/jsref_slice_array.asp

You can see that the slice method select object et throw them into a new array object ^^ So you can't delete an object like this, may be you can try a thing like this :

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... */

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.