.slice() Method ~ How to Remove an Item from an Array

Tell us what’s happening:
After 12 failed attempts to pass this challenge, I had to see solutions which lead me to some confusion:
The solution:

return state.slice(0, action.index).concat(state.slice(action.index + 1, state.length));

I do understand the first part of slice()-ing before concat(), but for the the second part of code (inside concat's parameter) I got 2 question:

  1. What does the + 1 means in the beginIndex (first parameter) of the .slice method (*I know it adds +1 to the index number, but I mean what’s the logic behind it)?

  2. Which .length of the state is the endIndex (second parameter) referring to - the current (original/non-modified) state or the modified shallow copy of the current state (to whom we are concatenating)?

    Your code so far

const immutableReducer = (state = [0,1,2,3,4,5], action) => {
switch(action.type) {
  case 'REMOVE_ITEM':
    // Don't mutate state here or the tests will fail
    return state;

const removeItem = (index) => {
return {
  type: 'REMOVE_ITEM',

const store = Redux.createStore(immutableReducer);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36

Challenge: Remove an Item from an Array

Link to the challenge:

slice goes from first argument up until, but no included the second argument.

Let’s say index is 2.
Then state.slice(0, index) will return [0, 1]
And state.slice(index, state.length) will return [2,3,4,5].
As you want to remove the element and index position, you can just start at the next position:
state.slice(index + 1, state.length) will return [3,4,5].
Also second argument for slice is optional. If it’s not there, then slice will go until the end of the array.

Slice doesn’t mutate the original array. So length is always the same.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.