“对象不支持属性或方法‘查找’"在浏览器中 [英] "Object doesn't support property or method 'find'" in IE

查看:30
本文介绍了“对象不支持属性或方法‘查找’"在浏览器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script>

        $(document).ready(function () {


            var data = [{
                "Id": "SWE",
                "Country": "Sweden",
                "Population": 9592552
            }, {
                "Id": "NOR",
                "Country": "Norway",
                "Population": 5084190
            }];


            function display(e) {
                alert("E" + e);
                var countryData = data.find(function (element, index, array) {
                    return element.Id === e;
                });
                alert(countryData.Population);
            }
            display('SWE');


        });


    </script>
</head>
</html>

上面发布的代码在 Firefox 和 Chrome 上正常运行,但在 Internet Explorer 中出现错误.错误信息:

The code posted above is working properly on Firefox and Chrome but I get an error in Internet Explorer. Error message:

对象不支持属性或方法查找"

Object doesn't support property or method 'find'

推荐答案

这里有一个解决方法.您可以使用过滤器代替查找;但过滤器返回匹配对象的数组.find 只返回数组中的第一个匹配项.那么,为什么不使用过滤器如下;

Here is a work around. You can use filter instead of find; but filter returns an array of matching objects. find only returns the first match inside an array. So, why not use filter as following;

data.filter(function (x) {
         return x.Id === e
    })[0];

这篇关于“对象不支持属性或方法‘查找’"在浏览器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆