我想如果我保留原始引用而不是简单地获取索引
const selected = {id: "abcd123", quantity: "5"};
const location = [
{id: "abcd123", quantity: "3"},
{id: "abcd1234", quantity: "3"},
];
const filterLocation = location.filter(loc => loc.id === selected.id);
console.log(location.indexOf(filterLocation));
我希望它会记录 0
但它总是返回 -1
。它实际上是如何工作的?
最佳答案
首先 filterLocation
不包含任何与 location
相同的对象。它包括唯一具有相同 id
字段但 quantity
字段不同的对象。其次,indexOf
方法对于非标量参数不能正常工作。
关于javascript - 无法获取数组中对象的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48143250/