你誤解了.indexOf()。indexOf返回可以在數(shù)組中找到給定元素的第一個(gè)索引,或者-1如果它不存在。inindex的索引也是如此。簡(jiǎn)而言之,您將對(duì)象 ( ) 與數(shù)字 ( ) 進(jìn)行比較。countercounterscounterindex也就是說(shuō),它還取決于您在數(shù)組中搜索對(duì)象的方式。如果嘗試查找與某個(gè)鍵/值結(jié)構(gòu)匹配的對(duì)象,.indexOf由于您提到的原因?qū)⒉黄鹱饔?。正?Gabriele 所指出的,如果使用參考進(jìn)行搜索,它將起作用。如果使用引用不是一種選擇,作為替代,您可以使用.findIndex(), 或.map(),并根據(jù)其屬性之一查找對(duì)象,例如id.const counters = [{id: 1, value: 0}, {id: 2, value: 0}, {id: 3, value: 0}, {id: 4, value: 0}];const findBySameness = {id: 3, value: 0};const findByRef = counters[2];const indexBySameness = counters.indexOf(findBySameness);console.log('Index by sameness: ', indexBySameness); // Do not find the object indexconst indexByRef = counters.indexOf(findByRef);console.log('Index by reference: ', indexByRef); // Found the object index// If a reference to the object is not available you can use the following methods// With .findIndexconst index3 = counters.findIndex(item => item.id === findBySameness.id)console.log('Index: ', index3); // Found the object index// With .mapconst index2 = counters.map(item => item.id).indexOf(findBySameness.id);console.log('Index: ', index2); // Found the object index
Firestore 安全規(guī)則無(wú)法檢索 request.auth.token.name
胡子哥哥
2021-08-20 14:32:29