简体   繁体   English

从数组中删除元素不起作用

[英]removing an element from array not working

I'm trying an element from an array using the id of the element yet not successful.我正在尝试使用元素的 id 从数组中获取一个元素,但未成功。

constructor(props){
   super(props)
   this.state = {

     arr : [<Input id="1" />,<Input id="2" />,<Input id="3" />,<Input id="4" />],
     id: 5 

   }
 }

   addToArray(){
    let id = this.state.id
    let arr = this.state.arr
    let tmp = <Input id={id} />
    id = id + 1
    arr.push(tmp)
    this.setState({arr,id})

  }


   removeFromArray(){

     let idx = prompt("Enter index")

     let data = [...this.state.arr]
     let arr_two = []

      arr_two = data.filter(function( element ) {
           return (element.props.id !== idx );
        }); 

      this.setState({arr: arr_two})


}

render(

   return(){

    <Button onClick={() => {this. addToArray()}}>add to array</Button>
   <Button onClick={() => {this.removeFromArray()}}>remove from array</Button>

}
)

no matter what value the user enters, only the last element is being removed.无论用户输入什么值,都只会删除最后一个元素。 Let say the user inputs 2, the element with the id 4 is gets removed instead of 2 although it exists in the array.假设用户输入 2,id 为 4 的元素被删除而不是 2,尽管它存在于数组中。 How should I modify to resolve the issue我应该如何修改以解决问题

You have a number of syntax errors in your original code, and you're not following best practices (eg if you're referencing state when using setState , use the callback because setState is asynchronous), but I think the primary issue you were having is that you're comparing strings to numbers.您的原始代码中有许多语法错误,并且您没有遵循最佳实践(例如,如果您在使用setState时引用state ,请使用回调,因为setState是异步的),但我认为您遇到的主要问题是您将字符串与数字进行比较。

I refactored your code to use more current methods (ie ES6) and added parseInt to convert the input from window.prompt into a number that can be compared to your <Input /> component's id prop.我重构了您的代码以使用更多当前方法(即 ES6)并添加parseInt以将来自window.prompt的输入转换为可以与您的<Input />组件的id道具进行比较的number I also switched the explicit <Input /> declarations to use a numeric input, rather than a string.我还将显式<Input />声明切换为使用数字输入,而不是字符串。

Hit run and you'll see that it works as-is.点击运行,你会看到它按原样工作。

 const Input = ({ id }) => <div>{id}</div> class Foo extends React.Component { state = { arr: [ <Input id={1} />, <Input id={2} />, <Input id={3} />, <Input id={4} />, ], id: 5, } addToArray = () => { const arr = [...this.state.arr, <Input id={this.state.id} />] this.setState(prevState => ({ arr, id: prevState.id + 1 })) } removeFromArray = () => { const id = parseInt(window.prompt("Enter index")) this.setState(prevState => ({ arr: prevState.arr.filter(element => element.props.id !== id), })) } render() { return ( <div> <div> <button onClick={this.addToArray}>Add to array</button> </div> <div> <button onClick={this.removeFromArray}>Remove from array</button> </div> Array: {this.state.arr} </div> ) } } ReactDOM.render(<Foo />, document.getElementById("root"))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM