0

I have an array like this:

[{
    "type": "adserver",
    "stats": {
        "10021": 48082,
        "10023": 1741
    }
}, {
    "type": "adserver",
    "stats": {
        "10021": 12312,
        "10023": 23
    }
}, {
    "type": "adserver",
    "stats": {
        "10021": 53423,
        "10023": 54
    }
}, {
    "type": "adserver",
    "stats": {
        "10021": 12345,
        "10023": 12
    }
}]

I'm trying to use:

ng-repeat = "array | orderBy:'stats.10021'"

and it throws this error:

Syntax Error: Token '.10021' is an unexpected token at column 6 of the expression [stats.10021] starting at [.10021].

1
  • <div ng-repeat="single in array | orderBy:'stats.10021'"> <businesscard>{{single.type}} {{single.stats}}</businesscard> </div> Commented Jun 15, 2016 at 10:27

3 Answers 3

1

The error is related to the fact that you are trying to access an object property which keys are numbers. You have to access the properties with square parameters.

You cannot access object properties with dot notation which keys are numbers!

Try this instead:

ng-repeat = "(key, value) in array | orderBy:'stats['10021']"
Sign up to request clarification or add additional context in comments.

Comments

0

Javascript object property names can not start with a number so you can only access them as string name using [] notation

Try

orderBy:stats['10021']

Comments

0

jsfiddle

<div ng-app="myApp" ng-controller="Ctrl">
  <div ng-repeat="single in array | orderBy:'stats.10021'">
     <businesscard>{{single.type}} {{single.stats}}</businesscard>
  </div>
</div>

angular.module('myApp', [])
.controller('Ctrl', function($scope) {
    $scope.array = [{"type":"adserver","stats":{"10021":48082,"10023":1741}}, {"type":"adserver","stats":{"10021":12312,"10023":23}}, {"type":"adserver","stats":{"10021":53423,"10023":54}}, {"type":"adserver","stats":{"10021":12345,"10023":12}}]
});

output

adserver {"10021":12312,"10023":23}

adserver {"10021":12345,"10023":12}

adserver {"10021":48082,"10023":1741}

adserver {"10021":53423,"10023":54}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.