0

I have created an app with dynamic forms as explained in this tutorial: https://angular.io/docs/ts/latest/cookbook/dynamic-form.html

Now I want to load the questions from a json file on a server instead of hard coded questions.

The JSON looks like this:

{
 "ventureReason":[
  {
"label": "Venture Reason",
"controlType": "dropdown",
"options": [
  {
    "key": "solid",
    "value": "Solid"
  },
  {
    "key": "great",
    "value": "Great"
  }
]
 }
],
"blablabla": [
{
  "label": "Blablabla",
  "controlType": "textbox",
  "options": [
  ]
}
]
}

In the QuestionService I want to build the questions based on value of controlType. But item.controlType always returns undefined. data contains the whole json file.

export class QuestionService {

questionUrl:string = `db`;
data:any;
questions : QuestionBase<any>[] = [];

constructor(
    private http: HttpService
){}

getQuestions() {
    return this.http.get(this.questionUrl)
    .map(res => [res.json()])
    .map(data => {
        this.data = data;
        return this.buildQuestions(data);
    })
}

buildQuestions(data) {
console.log("data: "+ JSON.stringify(data));  //whole json file
    let questions = [];
    data.forEach((item: QuestionGroup) => {
        console.log("item: " + JSON.stringify(item)); //whole json file
        console.log("item: " + JSON.stringify(item.controlType));  //undefined
        if (item.controlType == 'dropdown') {
            item.controlType = 'dropdown';
            questions.push(new DropdownQuestion(item));
        }
        else if (item.controlType == 'textbox') {
            item.controlType = 'textbox';
            questions.push(new TextboxQuestion(item));
        }
    });
    return questions;
}

}

How could I acces the controlType to build the questions?

Matthew Green
  • 10,161
  • 4
  • 36
  • 54
maidi
  • 3,219
  • 6
  • 27
  • 55
  • Which part of your entire JSON object `item: QuestionGroup` should represent? I'm asking because both `console.log("data: "+ JSON.stringify(data));` and `console.log("item: " + JSON.stringify(item));` result in printing the whole json file (like you mentioned in your comments). – Emin Laletovic Apr 21 '17 at 15:20
  • one item should be `ventureReason` and the other `blablabla` – maidi Apr 21 '17 at 15:30
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Heretic Monkey Apr 21 '17 at 20:12

3 Answers3

0

Based on the JSON object you've posted, you need to do this:

    data.forEach((questionGroup: QuestionGroup) => {
       questionGroup.forEach((item: any) => { 
       //you can replace 'any' with the appropriate class that represents 'item'
          //access item properties, like controlType
          console.log(item.controlType);
       });
    });
Emin Laletovic
  • 4,084
  • 1
  • 13
  • 22
  • now the console.log of item isn't called at all but the console.log on data is executed 2 times – maidi Apr 21 '17 at 15:47
0

It looks like the item's value is an array of objects. Maybe I'm mistaken, but I think item[0].controlType should return the result for which you're looking.

Zach Newburgh
  • 563
  • 3
  • 11
0

Your problem is, when you look closely at your JSON, your data you want to access is inside an array ventureReason. You want the content of that array, so make a slight change to your mapping, and it should be all good!

.map(res => res.json().ventureReason) // extract the data from the array

so your item.controlType was undefined, since there was no property, since your singleitem actually contained only the property ventureReason and the whole array.

Here's

Demo

AT82
  • 71,416
  • 24
  • 140
  • 167