2

THis is the format of my JSON data:

[

 {

 "Department_id": "34",

 "Names": [

     "Jack Jones",

     "Matt Jones",

     "Lynn Lewis"

  ],

  "Employee-num": 3,

  "Salary-info": {

      "Jack Jones": 1000,

      "Matt Jones": 2920,

      "Lynn Lewis": 1500

    },

 },

]

How do you get the "salary-info"? And to display like the following:

Jack Jones: 1000

Matt Jones: 2920

Lynn Lewis: 1500

On jade, I try to access it using

p(ng-repeat='details in info.salary-info') {{ details }}

But it's not working and it only display a "0" on the screen.

info.Department_id works for displaying the department id of 34.

Problem solved.

The problem why nothing displayed is because of the hyphen exists on object names.

You need to access this kind of objects with a ' [" name of the object with a hyphen"] ',

like: {{ info["salary-info"] }}

instead of {{ info.salary-info }}.

Hopes it helps anyone having the same issue.

1
  • my guess is you could simplify api output for this ...people:[{name:'Jack',salary:100]}. Arrays can be sorted but objects can't. Also generally best not to use special characters in property names when it can be avoided Commented Sep 28, 2015 at 16:22

3 Answers 3

3

You can get keys and values from objects like so:

<span ng-repeat="(name, salary) in info['Salary-info']">{{name}} : {{salary}}</span>

https://docs.angularjs.org/api/ng/directive/ngRepeat#iterating-over-object-properties

Sign up to request clarification or add additional context in comments.

1 Comment

<span ng-repeat="(name, salary) in info["salary-info"] > {{name}} : {{salary}}</span>
2

Problem is the hyphen in Salary-info. For a demo, see this plunker.

index.html

<!-- Jack Jones: 1000 Matt Jones: 2920 Lynn Lewis: 1500 -->
<p ng-repeat="name in details.Names">
  {{name}}: {{details['Salary-info'][name]}}
</p> <!-- works -->

or

<p ng-repeat="(name, salary) in details['Salary-info']">
  {{name}}: {{salary}}
</p> <!-- works-->

or

<p ng-repeat="(name, salary) in details2.SalaryInfo">
  {{name}}: {{salary}}
</p> <!-- works -->

all works, but

<p ng-repeat="(name, salary) in details.Salary-info">
  {{name}}: {{salary}}
</p> <!-- fails!-->

fails.

Data

app.js

app.controller('MainCtrl', function($scope) {
  $scope.details =
      ...
        "Salary-info": {"Jack Jones": 1000, "Matt Jones": 2920, "Lynn Lewis": 1500},
      ...

  $scope.details2 =
      ...
      "SalaryInfo": {"Jack Jones": 1000, "Matt Jones": 2920, "Lynn Lewis": 1500},
      ...

Comments

2

The ngRepeat syntax for objects is (k, v) in obj - so you can do:

p(ng-repeat='(name, salary) in info.salary-info') {{ name }} : {{salary}}

3 Comments

this syntax is working for "Names" since it is an array but I tried it for salary-info. It's not working.
@Brother_MW -- The above is object syntax - it won't work for arrays.
not sure will evalute - in property name using dot notation and also data has cap S ... info['Salary-info'] might be better

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.