2

I can't iterate over an array of objects. In each object, I have a name, but I can't get the text of the object or the name inside it.

I have the following code in my ts file

retrieveMultiple(config, "accounts")
.then(
    (results) => {
        const accounts: any[] = [];
        for (let record of results.value) {
            accounts.push(record);
        }

        this.accounts= accounts;
        console.log(accounts[0].name);
        console.log(accounts);
    },
    (error) => {
        console.log(error);
    }

And I have this in my HTML file:

      <p>Accounts:</p>
  <ul>
    <li *ngFor="let account of accounts | json">
      {{ account.name }}
    </li>
  </ul>
2
  • Why are you using the json pipe? Commented Mar 2, 2019 at 21:15
  • why do you need json pipe here? Commented Mar 2, 2019 at 21:15

2 Answers 2

3

You should not have the json pipe in your loop expression. Change your output code to this:

<p>Accounts:</p>
<ul>
  <li *ngFor="let account of accounts">
    {{ account.name }}
  </li>
</ul>

or for debugging you could use this:

<p>Accounts:</p>
<ul>
  <li *ngFor="let account of accounts">
    {{ account | json }}
  </li>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

0

https://angular.io/api/common/NgForOf

retrieveMultiple(config, "accounts")
.then(
    (results) => {
        this.accounts = results.value;
        console.log(this.accounts[0].name);
        console.log(this.accounts);
    },
    (error) => {
        console.log(error);
    }
)
<p>Accounts:</p>
<ul>
  <li *ngFor="let account of accounts">
    {{ account.name }}
  </li>
</ul>

2 Comments

i have accounts = []; so account.value will not work
Then it will not display anything inside the <ul>

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.