How to declare an array in javascript that will receive an array of JSON

0

Given the interface:

export interface Pergunta {
    id: number
    titulo: string
    opcoes:[]
}

I want the array array to receive its array of options:

{
    "id": "1",
    "titulo": "Qual o seu comportamento em relação aos seus investimentos?",
    "opcoes": [
      {
        "description": "Preservar meu dinheiro sem correr risco"
      },
      {
        "description": "Ganhar mais dinheiro, assumindo riscos moderados"
      },
      {
        "description": "Ganhar mais dinheiro, assumindo riscos agressivo"
      }
    ]
  },
    
asked by anonymous 13.03.2018 / 20:16

2 answers

2
export interface Pergunta {
    id: number;
    titulo: string;
    opcoes: Opcao[];
}

export interface Opcao {
    description: string;
}

This would be the most correct way to receive your data

    
13.03.2018 / 20:53
0

I think the best solution is for you to 'type' the array of the options attribute. In some cases when you do not do this, you can use an array of any . For example:

public opcoes: Array<any>;

However, I think the best solution to your problem would look something like this:

export interface Pergunta {
    public id: number
    public titulo: string;
    public opcoes: Array<TipoDeOpcoes>;   } 

    export interface TipoDeOpcoes {
            public description: string   
}
    
13.03.2018 / 20:35