我有一个对象数组,并使用indexOf来查找该数组中的特定对象。寻找一个对象数组中的对象返回null
我已经切换到在线数据存储,因为所选对象的数据现在作为变量返回,所以我必须创建一个“虚拟”对象,然后用它来查找数组中匹配的选定对象。
我比较了虚拟对象和数组中的对象,它们看起来相同,但总是返回null。
这是对象的数组:
data:
[
Creative {
id: '5631943370604544',
gid: '5631943370604544',
name: 'Hello',
description: '',
url: '',
videos: []
},
Creative {
id: '5653294995210240',
gid: '5653294995210240',
name: 'Yo',
description: '',
url: '',
videos: []
},
Creative {
id: '5745189578604544',
gid: '5745189578604544',
name: 'Sup',
description: '',
url: '',
videos: []
}
]
这是虚拟对象:
object:
Creative {
id: '5653294995210240',
gid: '5653294995210240',
name: 'Yo',
description: '',
url: '',
videos: []
}
下面是用于查找函数。这是不可修改的,因为它是GraphQL和Relay的一部分,并且它是使用的函数。
function cursorForObjectInConnection(data, object) {
var offset = data.indexOf(object);
if (offset === -1) {
return null;
}
return offsetToCursor(offset);
}
因为'{name:“bob”} === {name:“bob”} // false' – vlaz