javascript - 如果 id 与另一个对象 id 相等,则 React 获取第二个对象
问题描述
data
如果此 id 与另一个对象的 id 相等,我正在尝试获取第二个对象。
使用下面的代码,我只能获得 ID 而不是整个
data: {
0: {
id: 1234,
name: 'Name 1'
},
2: {
id: 4321,
name: 'Name 2'
},
3: {
id: 876,
name: 'Name 3'
}
}
instanceID: 4321
constructor(props) {
super(props);
this.state = {
defaultInstance: 0
}
}
fetch('api)
.then(response => {
console.log('Data fetched', response);
this.setState({
defaultInstance: [response.data.indexOf(response.data.find((instance) => instance.id === response.instanceID))]
});
});
所以,我想要实现的是添加到defaultInstance
那个对象
2: {
id: 4321,
name: 'Name 2'
},
解决方案
尝试这个 :
defaultInstance: response.data.filter(d=> d.id == response.instanceID)
推荐阅读
- html - 使用高于导航栏的图像
- html - 让 gif 一个接一个地延迟播放
- yocto - 覆盖配方的兼容性
- c# - 从外部服务器访问 IDP 发起的登录
- r - rvest 数据抓取用 NA 替换丢失的 html_node
- reactjs - Promise in UseEffect 卸载
- apache-kafka - 我可以使用 __consumer_offsets 主题来检查消息是否已被消费?
- c++ - 如何在使用带有自定义池分配器的 std::vector 时摆脱无用的分配和构造?
- apache-spark - 如何在 spark sql 的 like 子句中有 100 多个任意大小的单词?
- python - 关于 __main__ 属性的困惑