Extracting information from console output and displaying it in a table with angular2

https://i.stack.imgur.com/BMt6J.pngI am facing an issue with retrieving data from the console output and populating it into an HTML table. Can someone please assist me with this? Here is the HTML Code:

<table class="table">
   <tr>
     <th>Completed On</th>
     <th>ID</th>
     <th>ParticipantId</th>
   </tr>
   <tr *ngFor="let data of result">
     <td>{{ data.completedOn }}</td>
     <td>{{ data.id }}</td>
     <td>{{ data.participantId }}</td>
   </tr>
</table>

And here is the TS Code snippet:

public result:any = [];

this.service
            .getParticipent(comparedCampaign.id,token)
            .subscribe(
                participent => {
                    this.loading = false;

                    var survey = this.surveyResults;
                    if(participent.length > 0) {
                        var data =[];
              participent.map(function(item) { 
                var list = survey.filter(function(result) {
                    if(result.participantId) {
                        return result.participantId == item.id
                    }
               })
                data.push(list);
                 this.result = data;
             })

                    } else {
                        alert('No Participent found!');
                    }
                }, error => {
                    console.log(error)
                })
    }
}

The objective is to fetch CompletedOn, ID, and ParticipantId values from the JSON Console output provided below and display them in the table.

JSON Console Output:

 [[{"satisfaction":"[object Object]","completedOn":"2017-08-22T08:52:55.788Z","id":3,"participantId":217},{"satisfaction":{"price":"less","recomondation":"1","winback":"reason6"},"completedOn":"2017-08-22T09:12:40.800Z","id":4,"participantId":217}, ... ]]

Encountering an error message: "EXCEPTION: Cannot set property 'result' of undefined"

Any assistance would be greatly appreciated. Thank you in advance!

Answer №1

To get the desired result, you first have to access the 0th index of the array and assign it accordingly,

this.service.getParticipent(comparedCampaign.id,token).subscribe(
participent => {
   this.result = participent[0];
}

After that, you can loop over the data and display it as specified below,

 <tr *ngFor="let data of result">
      <td>{{ data.completedOn }}</td>
      <td>{{ data.id }}</td>
      <td>{{ data.participantId }}</td>
 </tr>

Answer №2

It seems like the array is being displayed in an incorrect format, so please give this a try

this.service.getParticipants(comparedCampaign.id, token).subscribe(
participant => {
   this.result = participant;
}

Then you can use your array in the following way

<tr *ngFor="let data of result">
      <td>{{ data?.completedOn }}</td>
      <td>{{ data?.id }}</td>
      <td>{{ data?.participantId }}</td>
 </tr>

Additionally, I suggest printing the entire array in HTML for better clarity like this

{{result | json}}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Uploading pictures to a directory and storing their filenames in a database with the help of PHP

I'm facing a challenge trying to upload 2 files into a folder and save their names and image paths in a database. Below is my HTML code for reference: <input class="field2" type="file" name="file[]" multiple="multiple" /> Here is the PHP code ...

The data type 'Observable<{}[]>' cannot be matched with the type 'AngularFireList<any>'

I have been learning how to use AngularFire2 by following this tutorial. Encountered the error: 'Type 'Observable<{}[]>' is not assignable to type 'AngularFireList'. Property 'query' is missing in type 'Observa ...

Locate and modify a single item in a list using NGRX

I have an array of items stored in my ngrx/store. When the user clicks on a button, I need to retrieve a specific item based on its id and update its properties without using ngxr/entities. I have managed to achieve this functionality in my current imple ...

Discover the best ways to repurpose an HTML page with varying jQuery/PHP code

After recently venturing into web development, I decided to create a guestbook that requires login information. Users with valid accounts have the ability to log in/out, create new entries, and edit their own content. They also have the option to change th ...

I am attempting to create a footer with a set size, but encountering an issue

I'm in the process of developing a responsive website using Vue.js. One aspect I am working on is the footer container, which looks fine on the full screen but shrinks when the screen resolution is reduced below 1100 pixels. As shown in the images li ...

Update the appearance of a cell if the value within it is equal to zero

I have discovered a way to achieve this using inputs. input[value="0"] { background-color:#F7ECEC; color:#f00;} Now, I am looking for assistance in applying the same concept to table cells. Can anyone provide guidance? Thank you. ...

Footer suffering from image invisibility syndrome

Having trouble displaying two social media images in my footer. Despite including them in my image map correctly, they just won't show up on the website. I've tried a lot of different things, but nothing seems to be working. Could it have somethi ...

TimeoutException occurs in Browser when using Visual Studio with ASP.NET and Angular

After waiting for 60 seconds, an error message appeared in the browser when trying to debug. However, after refreshing the browser window and waiting for another 15 seconds, everything seemed to be working fine. An unexpected error occurred during the r ...

What is the maximum size allowed for a background image in CSS?

Within a 70 by 50 px box, I have various SVG images with different aspect ratios - some portrait and others landscape. Setting background-size: 70px auto; works for landscape images but distorts the portraits by stretching them vertically. Is there a way t ...

Typescript struggling to comprehend the conditional rendering flow

I am facing an issue with the code snippet below: import * as React from 'react' type ComponentConfig = [true, {name: string}] | [false, null] const useComponent = (check: boolean): ComponentConfig => { if (check) { return [true, {name ...

Exploring Child Elements in Angular 2 Templates

I am working on a component and I need to access some child nodes from the template. I was able to access the details div, but I'm not sure why the code is functioning as it does. Can someone please explain what the Future class does? Also, why is the ...

The chosen date is not preserved within the select box

I have these specific items: $scope.items = [{"date":"2014-12-26T05:00:00.000Z","display":"6:00"}, {"date":"2014-12-26T05:15:00.000Z","display":"6:15"}, {"date":"2014-12-26T05:30:00.000Z","display":"6:30"}] When I use the following code: <select ...

Node_modules seem to be missing

After completing the TypeScript 101 QuickStart tutorial using Visual Studio 2015 and Node Tools for Visual Studio, I attempted to import the 'winston' npm module. However, no matter what path I specify, Visual Studio indicates that it cannot loca ...

Troubleshooting issue in Angular 6 mat-select: original array not resetting after filtering values

When filtering an array based on multiple selections from a mat-select, everything works smoothly except for one issue - if I select an option and then deselect it, the entire array disappears from the UI. However, if I select a few other options after tha ...

Navigating through nested JSON Objects for dropdown functionality in Angular 6 - a step-by-step guide

Currently, I am facing a challenge in Angular 6.0 where I am trying to use HttpClient to iterate through JSON data retrieved from a local file within my assets folder. Below is the sample JSON Data: [{ "configKey": [{ "user1": [{ ...

A triangular-shaped div positioned at the bottom with a captivating background image

Is there a way to create a div with a unique twist - a triangle shape at the bottom? I've been attempting to add a background image within the triangle using a pseudo element (:after), but so far, it hasn't been successful. #homebg:after{ co ...

Exploring abstract classes for diverse implementation strategies?

Consider the following scenario: export abstract class Button { constructor(public config: IButton) {} abstract click(); } Now, we have a concrete class: class ButtonShowMap extends Button { private isShow = false; constructor(public config: IBu ...

Something is amiss with the PHP email validation functionality

I have been facing issues with the functionality of my form that uses radio buttons to display textboxes. I implemented a PHP script for email validation and redirection upon completion, but it doesn't seem to be functioning correctly. The error messa ...

Is it possible to receive a notification when the DOM elements of an Angular 2+ component are successfully rendered in the browser?

I have a parent component in Angular that contains over 1000 DOM nodes. These nodes are rendered using two ngFor cycles, resulting in a 2D table structure. Each of these DOM nodes is an individual Angular component. The styles and displayed values of these ...

Steps for configuring jQuery hide(), adding Class, and remove Class for flawless execution

My HTML Code is structured as follows : ... @foreach($hotel as $key=>$value) ... <div class="iteration"> <input type='hidden' value='<?php echo $value['HCode'].'#' ...