0

I get an object from an array of elements, and I need to parse it as an array and display it as a list, can you tell me how to do this? I only know how to handle requests.

html:

<ul *ngFor="let filtered of reposFiltered">
              <li>
               {{filtered.name}}
               {{filtered.description}}
               {{filtered.language}}
               {{filtered.html}}
               </li>
            </ul>

.ts:

  // Required data
    selectRepo(data){
      this.reposFiltered = data;
      console.log(data)
    }

massive with objects:

0: cu {name: "30daysoflaptops.github.io", description: null, language: "CSS", html: "https://github.com/mojombo/30daysoflaptops.github.io"}
1: cu {name: "asteroids", description: "Destroy your Atom editor, Asteroids style!", language: "JavaScript", html: "https://github.com/mojombo/asteroids"}
etc

1
  • what is the cu in the objects array. This is wrong syntax for an Object Commented Aug 10, 2021 at 15:02

2 Answers 2

1

If you only wish to see the data in the rows you could use the following code:

// the data
const data = [
    {name: "30daysoflaptops.github.io", description: null, language: "CSS", html: "https://github.com/mojombo/30daysoflaptops.github.io" }, 
    {name: "asteroids", description: "Destroy your Atom editor, Asteroids style!", language: "JavaScript", html: "https://github.com/mojombo/asteroids"}
];

// Required data
selectRepo(data){
  this.reposFiltered = data.map(row => Object.values(row));
}

//html 
<!-- iterate through all data rows --> 
<ul *ngFor="let row of reposFiltered">
    <!-- iterate through all values -->
  <li *ngFor="let value of row">
    {{ value }}
    </li>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

0

To iterate over the properties on an object you need to use the https://angular.io/api/common/KeyValuePipe

something like this:

<ul *ngFor="let filtered of reposFiltered | keyvalue">
  <li>
    {{filtered.value.name}}
    {{filtered.value.description}}
    {{filtered.value.language}}
    {{filtered.value.html}}
  </li>
</ul>

If you need to access the key:

{{filtered.key}}

This code is useful for debugging:

{{filtered.key | json}}
{{filtered.value | json}}

Comments

Your Answer

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