我有這個函式,它迭代陣列中的每個物件以獲取它們的 id,然后使用給定的 id 過濾結果:
const id = another.id; //variable used to filter through the items
const pos = this.items.map(function(e){return e.id}).indexOf(id); //filter items with another.id
this.items.splice(pos, 1) //remove item with the same id as another.id
Theanother.id有時是 astring有時是 a number,當型別是時它會起作用,string但pos會變成-1表明indexOf()沒有找到匹配項。的e.id也是一種型別string。
所以我的問題就像標題所說的那樣,indexOf()型別敏感嗎?
uj5u.com熱心網友回復:
是的,根據mdn是型別敏感的:
indexOf() 使用嚴格相等(與 === 或三重相等運算子使用的方法相同)將 searchElement 與 Array 的元素進行比較。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/383202.html
標籤:javascript 数组
