-1
let fish = [ "piranha", "barracuda", "koi", "eel" ];

// Remove two items, starting at index position 1
fish.splice(1, 2);

fish;

Since the first digit 1 represents position "barracuda" within the array and 2 represents the items to be removed, Why doesn't this remove "koi" and "eel" (the 2 positions that follow) but instead removes "eel" and "piranha" (the 2nd and 3rd in rotation)?

cigien
  • 57,834
  • 11
  • 73
  • 112
  • Please don't vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed, and thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](/help/what-to-do-instead-of-deleting-question). – cigien Sep 09 '21 at 23:38

1 Answers1

1

Why doesn't this remove "koi" and "eel" (the 2 positions that follow)

Why would it? Look at the array indexes and the values at these positions:

fish[0]    "piranha"
fish[1]    "barracuda"
fish[2]    "koi"
fish[3]    "eel"

fish.splice(1, 2) says (as you yourself wrote) that starting with position 1 it should remove 2 elements.

So, at position 1 we have barracuda, which gets removed. But you said to remove 2 elements, not 1, so it removes another one: At position 1 we now have koi, which gets removed as well.

End result is that barracude and koi got removed, and we are left with ["piranha", "eel"] as expected.

You could imagine the way splice works like this:

// Define `remove` and `insert` primitives.
// Yes I know I'm now explaining `splice` with `splice`... but just think about
// those as basic ways to remove (and return) or insert one element at a given
// position.
const remove = (array, index) => array.splice(index, 1)
const insert = (array, index, el) => array.splice(index, 0, el)

// Our own implementation of `splice`, using only the remove/insert primitives.
// I'm adding the array itself as an additional parameter at the front.
function ourSplice (array, position, removeCount = null, ...newElements) {
  // Note: This does not simulate the behavior of splice(), splice(undefined),
  // splice(undefined, undefined) and similar things, which don't behave all the
  // same. It assumes at least a valid position.

  // Negative position means N elements from the *end* of the array.
  if (position < 0) position += array.length

  // Unspecified remove count means to remove all elements starting with the given
  // position (note that in the real splice function, this works only if the
  // argument is omitted, not when it is undefined or null)
  if (removeCount === null) removeCount = array.length - position

  // Remove the given number of elements starting at the given position
  const removed = []
  for (let i = 0; i < removeCount; i++) {
    removed.push(remove(array, position))
  }

  // Add any new elements given at the given position
  for (let i = 0; i < newElements.length; i++) {
    insert(array, position + i, newElements[i])
  }

  // Return the removed elements
  return removed 
}
CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • 1
    totally misread the results... I was seeing "eel" and "piranha" being removed rather that correctly as them being left in... my mistake. – Vince Caruso May 31 '20 at 18:33
  • 1
    Ah that's probably because `splice` _returns_ the removed elements. So you probably looked at the return value instead of the array's new contents. – CherryDT May 31 '20 at 19:09