Home > OS >  Issue with creating array inside array in angular 8 model
Issue with creating array inside array in angular 8 model

Time:03-23

I am trying to create a model named "reportVersion"

This contains an array (ReportCategory) that contains objects collection and two variables one is numeric and another is a string.

I am creating this model just to pass to an API, as a parameter body.

Since I have not created a model in the submission form, in the same format as like needs to be passed as a parameter to an API. I need to do fetch values from form Model and push it to reportVersion. Further, I will pass reportVersion to the API as a parameter to call the api.

Currently, I facing issues while building reportVersion. I have put the code below in stackblitz.

I don't understand why console.log(model) is not printing to console. Please help.

  this.reportCategory.push({ categoryName: 'demo1', weightage: 10 });

    this.reportCategory.push({ categoryName: 'demo2', weightage: 20 });

    this.reportCategory.push({ categoryName: 'demo3', weightage: 30 });

    this.reportCategory.forEach((element) => {
      this.reportVersion['categories'].push({
        categoryName: element.categoryName,
        weightage: element.weightage,
      });
    });
    console.log('this is final output');
    console.log(this.reportCategory);

https://stackblitz.com/edit/angular-ivy-vu99yk?file=src/app/hello.component.ts

CodePudding user response:

You'd need to initialize the reportCategory before using methods on it (like push).

this.reportCategory = []; // Added this line
this.reportCategory.push({ categoryName: 'demo1', weightage: 10 });
this.reportCategory.push({ categoryName: 'demo2', weightage: 20 });
this.reportCategory.push({ categoryName: 'demo3', weightage: 30 });

It worked in the StackBlitz in my side :)

// Dylan

  • Related