我遇到以下情况,需要删除索引。
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])
}
/>
最佳答案
您可以使用 slice 删除最后一个元素
this.state.rangeValue.slice(0,-1);
您的代码:
<Range
defaultValue={
this.props.rangeValue[2] ? [...this.state.rangeValue] : this.state.rangeValue.slice(0, -1)
}
/>
关于javascript - 使用 JS 使用剩余/扩展运算符删除索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57825083/