-1

I was trying to use the splice() function to manipulate an array of integers. But all I am getting is strange results.

Here is my code

var g = [0,1];
//Expected result [0, 1, 1]
//Actual result [0]
console.log(g.splice(0, 1, 0, 1));
//Expected result [0,5]
//Actual result [1]
console.log(g.splice(1, 1, 5));

Can any of you help me understand the usage of splice on integer array?

Thanks in advance.

1 Answers1

0

Here's a link that should help. Basically the first element is index to insert the next param is how many elements to remove the next arguments are the elements to add to the array.

Description Javascript array splice() method changes the content of an array, adding new elements while removing old elements.

Syntax Its syntax is as follows −

array.splice(index, howMany, [element1][, ..., elementN]); Parameter Details index − Index at which to start changing the array.

howMany − An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed.

element1, ..., elementN − The elements to add to the array. If you don't specify any elements, splice simply removes the elements from the array.

Return Value Returns the extracted array based on the passed parameters.

Jonathan Van Dam
  • 630
  • 9
  • 23
  • Link-only answers are bad. Imagine the link is broken, no one can understand what you're talking about. Hence link-only. – user202729 Feb 04 '18 at 05:03
  • @jonathan Have you test the code yourself? I read the documentation on [mozilla developer site](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice). I want to know if this work for an array of integer? Thank you for everyone's help. – Eddy Zhuo Feb 05 '18 at 03:46