我有一个对象数组,我需要从中找到特定索引,然后从数组中删除相应的对象。
我使用了findIndex()
方法:
var index = data.findIndex(x => x.OBJECTID == objectID);
data.splice(index, 1);
这在 Chrome 中工作正常,但在 IE 11 中,它会抛出
object doesn't support property or method 'findIndex'
我通过使用此 stackoverflow 答案中的解决方案解决了这个问题:findIndex() method issue with internet explorer
在检查控制台日志时,我注意到 Chrome 和 IE 11 中的对象数组之间存在差异:
在 IE 11 中,控制台日志中,对象数组为: enter image description here
在 Chrome 中,它是: enter image description here
请注意,Chrome 中的对象属于 object 类型,但在 IE 中,它们属于 [object object] 类型。这就是 findIndex() 方法失败的原因吗?如果是,为什么类型不同,如果不是,为什么 findIndex() 在 IE 11 中失败?
最佳答案
我相信你的问题不仅是findIndex,还有I.E.不支持 lambda 函数。
为了支持 IE,使用 for 循环是不可避免的。或者您可以使用lodash findIndex 的版本
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
_.findIndex(users, function(o) { return o.user == 'barney'; });
关于javascript - 为什么 Internet Explorer 11 对象不支持属性或方法 'findIndex',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44956938/