0

so i just noticed that when i use Array.splice() it's like i'm using Array.slice(),

So when i type[0, 1, 2, 3].splice(0, 2) it returns [0, 1]? (yes i am shure that i'm typing splice not slice)

So basicly:

[0, 1, 2, 3, 4, 5].splice(0, 2);
returns [0, 1]?

[0, 1, 2, 3, 4, 5].splice(3, 1);
returns [3]?

[0, 1, 2, 3, 4, 5].slice(0, 2);
returns [0, 1]

[0, 1, 2, 3, 4, 5].slice(3, 1);
returns [3]

Why does this happen? It's supposed to remove the specified objects right?

DayDun
  • 59
  • 1
  • 9
  • `splice()` can replace items with other items, altering the input array. `slice()` just takes from an array without altering it. The docs must explain this better. – Rudie Nov 24 '15 at 21:41
  • Look at what the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)...."*Returns An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.*" It is not returning the altered array as you assume it is. – epascarello Nov 24 '15 at 21:46
  • Shouldn't it work like this: http://stackoverflow.com/questions/5767325/remove-a-particular-element-from-an-array-in-javascript ? – DayDun Nov 24 '15 at 21:47
  • See MDN's [`Array.prototype.splice()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) – Oriol Nov 24 '15 at 21:48
  • The second argument of Array.splice() is the delete count – DayDun Nov 24 '15 at 21:48

4 Answers4

0

slice takes a copy of a section of the array and returns it.

splice removes a section of the array and returns it. An optional parameter allows replacing those elements with others as well.

TbWill4321
  • 8,098
  • 2
  • 21
  • 22
0

The splice method modifies the contents of the array on which you are calling.

The splice() method changes the content of an array by removing existing elements and/or adding new elements.

Whereas, the slice method returns the new array copy from the given range of index.

The slice() method returns a shallow copy of a portion of an array into a new array object.

rajuGT
  • 5,784
  • 2
  • 22
  • 43
0

It returns the same thing, sure, except slice does not mutate the original array. Splice, on the other hand returns what was removed from the original array.

axelduch
  • 10,132
  • 2
  • 26
  • 49
0
  • splice() replaces items with other items, altering the input array.
  • slice() just takes from an array without altering it.

Since you're not using a variable as input, you'll never see the difference. But try this:

a = [4, 5, 6, 7, 8]
> [4, 5, 6, 7, 8]
a.splice(2, 2)
> [6, 7]
a
> [4, 5, 8]

a has changed. That's splice()

The real power comes from substitution/replacement:

a = [3, 4, 5, 6, 7]
> [3, 4, 5, 6, 7]
a.splice(2, 2, 500, 600)
> [5, 6]
a
> [3, 4, 500, 600, 7]

If you have an array of items to 'splice' into the array, you have to use .apply:

a = [3, 4, 5, 6, 7]
> [3, 4, 5, 6, 7]
b = [500, 600]
>  [500, 600]
a.splice.apply(a, [2, 2].concat(b))
> [5, 6]
a
> [3, 4, 500, 600, 7]

Or make your own function that does that more readable.

You can also splice-add items into an array, by making the 2nd argument 0: a.splice(2, 0, 500, 600).

You can read all this, and more, in the docs!

Rudie
  • 46,504
  • 37
  • 126
  • 167