2

I have array like this

this.location={"asdsad":{"asdsad":"qweqwe"}};

<li ng-repeat="(key, value) in location">
{{value | json}} 
</li>

Tried to print in html like above but it's not working.

James Z
  • 12,209
  • 10
  • 24
  • 44
Anil Rayat
  • 49
  • 2
  • 8
  • ng-repeat does not exist in angular 6. Ty to use *ngFor. https://stackoverflow.com/questions/41396435/how-to-iterate-object-keys-using-ngfor/41396558 – Abhay Dec 01 '18 at 06:54
  • I don't see any array here. The value of `this.location` is an object. – Henry Dec 01 '18 at 07:12

3 Answers3

3

If you're using Angular 6.1.0, you can use the keyvalue pipe:

<div *ngFor="let item of location | keyvalue">
    <p>{{ item.value | json }}</p>
</div>

Not originally my answer, found it here: access key and value of object using *ngFor.

codequiet
  • 1,102
  • 5
  • 9
0

Try to use Object.keys in the template and use it in *ngFor instead of ng-repeat.

 objectKeys = Object.keys;
 location = {'asdsad': {'asdsad': 'qweqwe'}};


 <li *ngFor="let key of objectKeys(location)">
  {{key + ' : ' + location[key]}}
</li>
Gregor Albert
  • 819
  • 1
  • 11
  • 23
  • looks like its working but not properly.. I have json object iike this `{ "name": "john", "properties": { "age": 25, "sex": "m" }, "salary": 1000 };` I get in result `john [object Object] 1000~ why object object is there. second thing i need to know how I can know this is object or not – Anil Rayat Dec 01 '18 at 07:38
  • Because "properties" IS an object. – Lazar Ljubenović Dec 01 '18 at 07:50
0

This code is work for me. i'm not sure it's best way to solve your problem.

component:

    location = {asdsad :{asdsad:"qweqwe"}};
     objectKeys = Object.keys;

Html:


     <li *ngFor="let key of objectKeys(location)">
            {{key + ' ' + (location[key].asdsad | json)}}
     </li>
Abhishek
  • 1,742
  • 2
  • 14
  • 25