0

I have the following scenario where I need an index deleted.

JS:

this.state = {
      rangeValue: this.props.rangeSlider && [
        this.props.rangeValue[0],
        this.props.rangeValue[1],
        this.props.rangeValue[2]
      ],
    };


<Range 
   defaultValue={
   this.props.rangeValue[2] ? [...this.state.rangeValue] : [...this.state.rangeValue] //minus last index in my array (this.props.rangeValue[2])
            }
          />
4
  • Why does it have to use the spread operator? Use the .slice() method: this.state.rangeValue.slice(0, -1) Commented Sep 6, 2019 at 15:49
  • What is <range Commented Sep 6, 2019 at 15:49
  • @Barmar perhaps they don't want the initial array modified. Commented Sep 6, 2019 at 15:53
  • @MathewBerg slice doesn't modify the original array. Before ES6 slices we used this.state.rangeValue.slice() to make a copy of an array. Commented Sep 6, 2019 at 15:57

1 Answer 1

1

You can remove the last element by using slice

this.state.rangeValue.slice(0,-1);

Your code :

<Range
  defaultValue={
    this.props.rangeValue[2] ? [...this.state.rangeValue] : this.state.rangeValue.slice(0, -1)
  }
/>
Sign up to request clarification or add additional context in comments.

3 Comments

Much Thanks @Taki
Spoke to soon. Getting this error with that condition: "Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node."

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.