我有一個對象數組,並使用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