Array.prototype.indexOf() 在 React.js 中工作是否不同

我试图了解这段代码是如何工作的,我正在使用 Array.prototype.indexOf() 来获取状态数组的索引,以便可以对其进行更新。我被告知在 indexOf() 的参数中传递一个对象,但我认为没有两个对象是相等的,那么 indexOf() 如何返回正确的索引我在这里遗漏了什么?这是我的代码


 handleIncrement(counter){//passing object here from child component


         const counters = [...this.state.counters];//cloning

         const index = counters.indexOf(counter)//geting index of param counter 

         console.log(counter === index)//how did index return true if this returns false???

        counters[index] = {...counter};// spread object onto index pos


        counters[index].value++;//increment value

        this.setState({counters})//updates state just fine no errors

     }


HUH函数
浏览 248回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript