Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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. But when I run the page on google chrome, I get the error "TypeError: response.sort is not a function"

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. Also this is happening only in google chrome, I tested it in firefox and IE10, and its working fine on these browsers. The json data I am receiving (response) has the following format

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

Could you please tell me how I can resolve this?

like image 932
akashrajkn Avatar asked Mar 15 '23 16:03

akashrajkn


1 Answers

Your response is not an Array. It could be an object with a property that contains the Array (e.g. 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.

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.

like image 135
Amir Popovich Avatar answered Mar 24 '23 17:03

Amir Popovich