【发布时间】:2022-01-20 02:32:11
【问题描述】:
我有这个函数迭代数组中的每个对象以获取它们的 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
another.id 有时是 string 有时是 number,当类型为 string 但 pos 将变为 -1 时它会起作用,这表明 indexOf() 没有找到一场比赛。 e.id 也是 string 的一个类型。
所以我的问题就像标题所说的那样,indexOf() 类型敏感吗?
【问题讨论】:
标签: javascript arrays