javascript - 为什么 Internet Explorer 11 对象不支持属性或方法 'findIndex'

标签 javascript arrays internet-explorer javascript-objects

我有一个对象数组,我需要从中找到特定索引,然后从数组中删除相应的对象。

我使用了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/

相关文章:

javascript - 如何在事件阶段更改时间线圆圈的颜色?

arrays - WordPress 插件设置类中的动态数组选项

java - 如何通过jdbc在postgres中获取数组基类型

c++ - 如何与 Internet Explorer C++ 交互

css - 侧面导航子菜单弹出按钮下推 IE7 中的其他菜单项

javascript - 在 IE 7/8 中使用 alt + shift + a 作为热键

javascript - 代码点火器

javascript - 我可以将引用尚未构建的字段的 JavaScript 放在哪里?

javascript - JS ES6 Promise 被无缘无故地调用

java - 将 PDF 文件转换为字节数组的方法..(使用 outputstream() 进行刷新)