9

I'm having trouble iteration a json object in the Ngfor, there is my template :

template:

<h1>Hey</h1>
  <div>{{ people| json}}</div>
  <h1>***************************</h1>
  <ul>
    <li *ngFor="#person of people">
        {{
          person.label
        }}
    </li>
 </ul>

people is the json object that I'm trying to iterate, I'm having rhe result of (people | json) and not getting the list, here is a screenshot:

and to finish, here is a part of json file :

{
"actionList": {
"count": 35,
"list": [
    {
    "Action": {
        "label": "A1",
        "HTTPMethod": "POST",
        "actionType": "indexation",
        "status": "active",
        "description": "Ajout d'une transcription dans le lac de données",
        "resourcePattern": "transcriptions/",
        "parameters": [
            {
                "Parameter": {
                    "label": "",
                    "description": "Flux JSON à indexer",
                    "identifier": "2",
                    "parameterType": "body",
                    "dataType": "json",
                    "requestType": "Action",
                    "processParameter": {
                        "label": "",
                        "description": "Flux JSON à indexer",
                        "identifier": "4",
                        "parameterType": "body",
                        "dataType": "json",
                        "requestType": "Process"
                    }
                }
            },

please feel free to help me

0

1 Answer 1

12

Your people object isn't an array so you can iterate over it out of the box.

There is two options:

  • You want to iterate over a sub property. For example:

    <ul>
      <li *ngFor="#person of people?.actionList?.list">
        {{
          person.label
        }}
      </li>
    </ul>
    
  • You want to iterate over the keys of your object. In this case, you need to implement a custom pipe:

    @Pipe({name: 'keys'})
    export class KeysPipe implements PipeTransform {
      transform(value, args:string[]) : any {
        if (!value) {
          return value;
        } 
    
        let keys = [];
        for (let key in value) {
          keys.push({key: key, value: value[key]});
        } 
        return keys;
      } 
    } 
    

    and use it this way:

    <ul>
      <li *ngFor="#person of people | keys">
        {{
          person.value.xx
        }}
      </li>
    </ul>
    

    See this answer for more details:

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

3 Comments

thank you a lot thierry, i've add the pipe as you mentioned, in my template, i've call it like this and still don't get anything : <li *ngFor="#person of people | keys "> {{ msg.value.Action.label }} </li> // for example getting all the labels under each action node
when i'm trying it like this : <li *ngFor="#obj of lists "> {{ obj.value.list[0].Action.label }} </li> i'm getting the label : A120, and not the first label A1, and i'm getting just a one element not a list, is it normal ??
i've tried to add the index into it like this, it did not work : <li *ngFor="#obj of lists | keys, #i=index "> {{ obj.value.list[i].Action.label }} </li> // how can i pass the index value inside the list ??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.