为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUH函数 2021-08-20 14:32:05
我试图了解这段代码是如何工作的,我正在使用 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     }
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 216 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信