简体   繁体   English

仅在Google Chrome中,angularjs中的“排序不是函数”错误

[英]“sort not a function” error in angularjs only in google chrome

I ran into this problem and am unable to figure out why and couldn't find any explanation on google. 我遇到了这个问题,我无法找出原因,也无法在谷歌上找到任何解释。 I have a $http.get method to retrieve some data from a url, on success i am sorting the response. 我有一个$ http.get方法从URL中检索一些数据,成功后我正在排序响应。 But when I run the page on google chrome, I get the error "TypeError: response.sort is not a function" 但是当我在谷歌浏览器上运行页面时,我收到错误“TypeError:response.sort不是函数”

var orderOfGroups = ["alpha", "beta", "gamma", "delta"];

$http.get(url)
.success( function (response, status, headers, config) {
    response.sort( function(a, b) {
        var aname = orderOfGroups.indexOf(a.team);
        var bname = orderOfGroups.indexOf(b.team);

        return bname-aname;
    });
});

I am getting the error at "response.sort" line. 我在“response.sort”行收到错误。 Also this is happening only in google chrome, I tested it in firefox and IE10, and its working fine on these browsers. 这也只发生在谷歌浏览器中,我在firefox和IE10中进行了测试,并且它在这些浏览器上运行良好。 The json data I am receiving (response) has the following format 我收到的json数据(响应)具有以下格式

[
    {
        team: "gamma",
        value: "p"
    },
    {
        team: "alpha",
        value: "q"
    },
    ......
]

Could you please tell me how I can resolve this? 你能告诉我怎么解决这个问题吗?

Your response is not an Array. 您的response不是数组。 It could be an object with a property that contains the Array (eg response.data ) or a string version of you array and then you will need to use JSON.parse and convert the string into an Array. 它可能是一个包含Array的属性(例如response.data )或数组的字符串版本的对象,然后您需要使用JSON.parse并将该字符串转换为Array。

Anyway, sort is on Array's prototype - that means that all the arrays will have that property on them - This is not related to AngularJS or google chrome . 无论如何, 排序是在阵列的原型-这意味着所有的阵列将对他们财产-这是相关的AngularJSgoogle chrome

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM