2
{  
   "status":"true",
   "select":[  
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHICH BRAND COUNTER IS IT?",
         "options":[  
            {  
               "option":"USPA"
            },
            {  
               "option":"HANES"
            },
            {  
               "option":"USPA & HANES "
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            }
         ]
      }
   ],
   "text":[  
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHAT IS THE OPENING STOCK OF INNERWEARS TOP?",
         "options":[  
            {  
               "option":"VEST"
            },
            {  
               "option":"BRA_PADDED"
            },
            {  
               "option":"BRA_NON PADDED"
            },
            {  
               "option":"CAMISOLE"
            },
            {  
               "option":"THERMAL"
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            }
         ]
      },
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHAT IS THE OPENING STOCK OF INNERWEARS BOTTOM?",
         "options":[  
            {  
               "option":"BRIEF"
            },
            {  
               "option":"TRUNKS"
            },
            {  
               "option":"PANTIES"
            },
            {  
               "option":"THERMAL LEGGINGS"
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            }
         ]
      },
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHAT IS THE OPENING STOCK OF COMFORTWEARS TOP?",
         "options":[  
            {  
               "option":"MEN'S T SHIRT"
            },
            {  
               "option":"MEN'S TANKS"
            },
            {  
               "option":"WOMEN'S T SHIRT"
            },
            {  
               "option":"WALL FIXTURE"
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            }
         ]
      },
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHAT IS THE OPENING STOCK OF COMFORTWEARS BOTTOM?",
         "options":[  
            {  
               "option":"BOXER SHORTS"
            },
            {  
               "option":"MEN'S SHORTS"
            },
            {  
               "option":"MEN'S PANTS"
            },
            {  
               "option":"MEN'S SOCKS"
            },
            {  
               "option":"WOMEN'S SHORTS"
            },
            {  
               "option":"WOMEN'S PANTS"
            },
            {  
               "option":"WOMEN'S CAPRI"
            },
            {  
               "option":"WOMEN'S SOCKS"
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            },
            {  
               "option":""
            }
         ]
      }
   ]
}

I have above JSON from API. I want to take Question and options to use it in ngFor for that I have written below code.

  if(this.user_type == 1){

        this.mwork = this.navParams.get('mwork');
        this.selectq = this.mwork.select;
        this.selecto = this.selectq.options;            
        this.textq = this.mwork.text;
        console.log(this.selecto);
    }

In console, if I wrote mwork. M getting data but when I wrote selecto. M getting options undefined. Is there anything wrong?. Please help.

EDITED: Used Ankit's answer below,

  if(this.user_type == 1){

        this.mwork = this.navParams.get('mwork');
        this.selectq = this.mwork.select;
        this.selecto = this.selectq[0].options;
        //this.selecto = this.selectq.options;
        this.textq = this.mwork.text;
        this.texto = this.textq[0].options;

        console.log(this.selecto);
    }

But now for all questions showing same options. How can i show as per question

Below is my HTML

  <div class="field-group" *ngFor="let mworkq of selectq">
        <div class="label-cust">{{mworkq.Question}}</div>
        <div class="field-cust">
          <select [(ngModel)]="selectedOption">
            <option value=''>Select</option>
            <option  *ngFor="let mworkq of selecto" value="{{mworkq.option}}">{{mworkq.option}}</option>

           <!--  <option value="{{mworkq.option4}}">{{mworkq.option4}}</option> -->

          </select>
        </div>
      </div>
      <div class="field-group" *ngFor="let mworkq of textq">
        <div class="label-cust">
              {{mworkq.Question}}
              <ion-input *ngFor="let mworkq of texto" class="{{mworkq.option}}" type="text" placeholder="{{mworkq.option}}" ></ion-input>

        </div>
Sagar Kodte
  • 3,723
  • 4
  • 23
  • 52

1 Answers1

4

The data type of this.selectq is array since this.mwork.select is array with values

"select":[  
      {  
         "view_":"select",
         "Type":"MARKET_WORKING",
         "Question":"WHICH BRAND COUNTER IS IT?",
         "options":[  
            {  
               "option":"USPA"
            }
         ]
    }
]

So the next line of code this.selecto = this.selectq.options; will have this.selecto as undefined as this.selectq.options is undefined since this.selectq is array and you need to access that with the index like this.selectq[0].options. Thus, change your code to,

this.selecto = this.selectq[0].options;

And now when you console log selecto it will surely not give you undefined

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62