Tips for accessing nested values post-subscription in Angular with JSON data?

I have implemented a getReports method that utilizes my web API's get method to retrieve JSON formatted responses.

Step1

    getReports() {
            return this._http.get(this.url)
                .map((response: Response) => response.json())
                .catch(this.handleError);  
        }

Step2

In the constructor of my component class, I inject the complete service and subscribe to this.reports within ngOnInit.

constructor(private _reportService: GetReports) {
    }
ngOnInit() {
       this._reportService.getReports().subscribe(reports => this.reports = reports);
}

Upon checking the console, I observe an array containing 127 records. My challenge lies in efficiently traversing this JSON data within the component to display nested values.

Image description available here

For instance, expanding the above array reveals data structured as follows:

0
     Key 1:" abdef",
     Key2 :[1,2,3 ]
     key 3:['test','test2']
1   
    Key 1:" check",
     Key2 :[1,2,3 ]
     key 3:['test3','test2']
 2 
     Key 1:" ghef",
     Key2 :[1,2,3 ]
     key 3:['test3','test2']
 ....
 ....
 ....
 127  
     Key 1:" check",
     Key2 :[1,2,3 ]
     key 3:['test4','test3']

I aim to extract all unique values for Key 1 across the 127 elements as shown above. Similarly, based on Key 1, I plan to identify distinct values under Key 3.

The objective is to retrieve all Key 3 values associated with Key 1 while avoiding duplicate entries.

While exploring resources like access key and value of object using *ngFor, I did not find a solution matching my requirements.

Your guidance on ways to effectively access or interpret nested JSON data in Angular would be highly appreciated.

Answer โ„–1

Retrieve unique keys

const uniqueKeys = reports.map((a) => a.key1).filter((item, pos, arr) => arr.indexOf(item) === pos);

Obtain all key3 values where key1 is equal to "abc"

let results = []
myarr.forEach((item) => {
    if(item.key1 === "abc"){
        results = results.concat(item.key3);
    }
})

results = results.filter((item, pos) => results.indexOf(item) === pos);

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

Search for particular words using pandas

I wanted to figure out a method to Success! Step 1) possibly tally all instances of specific text (e.g "็ฅž" or "ไปๆ•™") in a single json or csv file. import pandas as pd pd.options.display.max_rows = 10000 # read the file df = pd.read_csv("/mypath/resu ...

The attribute 'checked' is not a valid property for the 'TElement' type

Learning about typescript is new to me. I have a functional prototype in fiddle, where there are no errors if I use this code. http://jsfiddle.net/61ufvtpj/2/ But in typescript, when I utilize this line - if(this.checked){ it presents an error [ts] Pro ...

What causes the typescript error in my code: The argument being passed is either a string, an array of FilterData, an array of numbers, or an array of Moments, which is not compatible with a parameter of type 'string'?

When writing my code, I have the need to utilize various types for different scenarios. Depending on the situation, the type may be a string, number[], FilterData[] (a custom type), or Moment[]. To address this requirement, I defined the type as: export ty ...

How can a parent's method be activated only after receiving an event emitter from the child and ensuring that the parent's ngIf condition is met?

Every time the element in the child template is clicked, it triggers the method activateService(name) and emits an event with the name of the selected service using the event emitter serviceSelected. The parent component's method scrollDown(name) is t ...

Ways to assign unpredictable values (such as ids, dates, or random numbers) to a Domain Entity or Aggregate Root when it has been injected as dependencies

I am currently developing a frontend repository that follows an innovative hexagonal architecture approach with domain-driven design principles, and it utilizes Redux Toolkit. The development process involves Test-Driven Development (TDD) where I employ c ...

Is there a way to search for text and highlight it within an HTML string using Ionic

Welcome everyone! I am currently utilizing Ionic to load an article from a local HTML string. <ion-content padding> <p [innerHTML]="url"></p> </ion-content> The 'url' variable contains the local HTML string. My goal ...

Angular Material 7 now allows you to create a link for an entire row!

Currently, I am utilizing Angular Material 7 tables (mat-table) and my goal is to create a link for an entire row so that it leads to a detailed page display. The specific requirement is to have a functional link that can be opened in a new tab, which mea ...

What is the most effective method for retrieving fresh content through JSON?

What is the most efficient method for retrieving new content on a website using JSON? I have come up with the following system: (looped) client -> (check for new content) -> server client <- (no new content available) <- server client -> ...

Error encountered due to WCF's JSON error handler triggering an exception

In the endpoint configuration, I have set up two behaviors: The first behavior is for json serialization and it closely resembles the example provided here. The important part of this behavior is as follows: public class NewtonsoftJsonBehaviorExtensio ...

How to ensure Service is loaded before App Component in Angular 6?

My Data service is responsible for fetching the JSON Object value, however all components load before the data service finishes loading. This results in undefined values when I call the service method from components. ...

Struggling to develop a C# (asmx) Web service utilizing the POST method and accepting FormData input

Could you kindly share a link with me so that I can follow it to create my first web service? This service needs to take data in form data and output it in JSON using the POST method. I am developing this service for both Android and iOS apps. Note: I am ...

List out all attributes and their corresponding values in an asymmetric JSON structure

I am dealing with a complex and lengthy JSON object, and my goal is to display all the attributes and corresponding values for the final endpoints (leaves) of the object. An example structure could be: data = { "Response": { "Version": "2.0", ...

Enrolling a new plugin into a software repository

I have 5 unique classes: ConfigManager ForestGenerator TreeCreator NodeModifier CustomPlugin My goal is to create an npm module using TypeScript that incorporates these classes. The main feature I want to implement is within the ConfigManager clas ...

What is the best way to access buffer data in TypeScript for Solana?

Is there a way to retrieve buffer data from TypeScript? I am attempting to use the public key to access all of my token lists, but I am only getting back an empty array of objects. import {Connection, Keypair} from "@solana/web3.js"; const Sola ...

Losing a specific characteristic of data occurs when assigning a JavaScript object

After dedicating a full day to investigating this case, I found myself losing hope. const Tests = (state = INIT_STATE, action) => { switch (action.type) { case GET_TEST_DETAIL: return { ...state, test: {}, error ...

Determine the category of a container based on the enclosed function

The goal is to determine the type of a wrapper based on the wrapped function, meaning to infer the return type from the parameter type. I encountered difficulties trying to achieve this using infer: function wrap<T extends ((...args: any[]) => any) ...

Is it possible to utilize Angular's structural directives within the innerHtml?

Can I insert HTML code with *ngIf and *ngFor directives using the innerHtml property of a DOM element? I'm confused about how Angular handles rendering, so I'm not sure how to accomplish this. I attempted to use Renderer2, but it didn't wor ...

Leveraging Angular Firebase MatTable with the power of 2 observables in 1

I'm currently facing an issue with my Firebase database data structure where I have a reference to a user id. Here's an example of the original data in my collection: { city: new york, country: usa addedBy: feibf78UYV3e43 // This is the USER ID ...

Prevent IonContent from scrolling to the bottom or top when using Ionic framework

In my Ionic app, I have a long text page with 2 buttons that trigger the actions scrollToBottom and scrollToTop. Due to the length of the page, I have set the scroll duration to be 30 seconds. I am facing two issues here: How can I stop the scrolling ...

I'm having trouble with one of my filter pipes not displaying any results. Can anyone help me troub

I have recently included a new filter for DL, but it seems that the results are not showing up as expected. Any ideas on what changes I should implement? <div class="form-group float-left mr-4"> <strong>DL</strong> <br /> ...