1

I'm trying to print this object:

export class Ricette {
    nome: string;
    azioni: {tempo: number, azione: string}[][]
  }
export const RICETTE : Ricette[] = [
    {
        nome: 'Alici',
        azioni: [[
            {tempo: 500, azione: 'scongela'},
            {tempo: 60, azione: 'nulla'}        
        ]]
    },
    {
        nome: 'Baccalà',
        azioni: [[
            {tempo: 500, azione: 'scongela'}],
            [
                {tempo: 210, azione: 'immerso'},
                {tempo: 210, azione: 'cestello su'},
                {tempo: 30, azione: 'immerso'}
            ]        
        ]
    },
    {
        nome: 'Hamburger',
        azioni: [[
            {tempo: 500, azione: 'scongela'}],
            [
                {tempo: 210, azione: 'immerso'},
                {tempo: 210, azione: 'cestello su'},
                {tempo: 30, azione: 'immerso'}
            ]        
        ]
    }
];
ricette = RICETTE;

in an html file.

the array azioni[] is going to have different length in every item of ricette.

what can I do for print the object in a html <'ul><'li><'/li><'/ul>?

2 Answers 2

2

You can add parent div with *ngFor in order to display all values of the array like following:

  <div *ngFor="let ricetta of ricettaArray">
        <h2>{{ricetta.nome}}:</h2>
        <div>
            <span>Nome-> </span>{{ricetta.nome}}</div>
            <ul *ngIf="ricetta?.azioni && ricetta?.azioni.length > 0">
                <li *ngFor="let item of ricetta?.azioni ; let i = index" [attr.data-index]="i">
                    {{item?.tempo}}
                    {{item?.azione}}
                </li>
            </ul>
    </div>

the ricettaArray declaration looks as following:

 ricettaArray : Ricette[] = [];

  let r1 = {
      nome: 'Alici',
      azioni: [
        {tempo: 500, azione: 'scongela'},
        {tempo: 60, azione: 'nulla'}]
    };
    let r2 = {
      nome: 'Blici',
      azioni: [
        {tempo: 400, azione: 'bcongela'},
        {tempo: 50, azione: 'bulla'},
        {tempo: 500, azione: 'bulldsfa'}]
    };
    this.ricettaArray.push(r1);
    this.ricettaArray.push(r2);
Sign up to request clarification or add additional context in comments.

2 Comments

it works pretty well, thanks. can you edit the last part of the code?
@AndreaFavero Sorry I've inserted my answer to not right place. Sorry, bat7. Now I've updated my answer
1

There is no such type:

azioni: {tempo: number, azione: string}[]

But there is a such type and its name is object:

azioni: {tempo: number, azione: string}

You can use *ngFor to show the following format data:

ricetta : Ricette = {
    nome: 'Alici',
    azioni: [
      {tempo: 500, azione: 'scongela'},
      {tempo: 60, azione: 'nulla'}]
  }

and HTML:

div>
    <span>Nome-> </span>{{ricetta.nome}}
</div>
<ul *ngIf="ricetta?.azioni && ricetta?.azioni.length > 0">
    <li *ngFor="let item of ricetta?.azioni ; let i = index" [attr.data-index]="i">
        {{item?.tempo}}
        {{item?.azione}}
    </li>
</ul>

UPDATE: You can use the second loop:

<div *ngFor="let ricetta of ricettaArray">
            <h2>{{ricetta.nome}}:</h2>
            <div>
                <span>Nome-> </span>{{ricetta.nome}}</div>
                <ul *ngIf="ricetta && ricetta?.length > 0">
                    <li *ngFor="let item of ricetta?.azioni;">
                        {{item?.tempo}}
                        {{item?.azione}}

                        <p> The second loop: </p>
                        <ul>
                            <li *ngFor="let subItem of item"> 
                               {{subItem.tempo}} 
                               {{subItem.azione}}
                            </li>
                        </ul>
                    </li>
                </ul>
        </div>

5 Comments

thanks. I have edited my question. I have modified the object. can you please help Me?
@AndreaFavero Yeah, but there is no such type: azioni: {tempo: number, azione: string}[] ? What type do you want to use? and could you fill the sample data?
azioni[] is an array and inside it there is an array of object
I've included the full data in the question
@AndreaFavero I've updated my answer. Sorry, I've edited bat7 answer and put my code to bat7 answer, not my. I was very hurry. Sorry:). Could you reconsider my updated answer?

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.