How can I extract a specific data value from a JSON file in Ionic 2?

Here is the JSON data:

[
  {
    "id": 1,
    "label": "saw",
    "total": "100"
  },
  {
    "id": 2,
    "label": "saw1",
    "total": "300"
  },
  {
    "id": 3,
    "label": "saw2",
    "total": "400"
  }
]

Below is my Typescript code snippet:

this.http.get('http://www/upnionic/chart.php')
  .map(res => res.json()).subscribe(data => {
    this.qwerty = data; this.qwerty.forEach(element => {
        let product = element.total;
        this.pieChartData = product; 
        console.log(product);
    }); 
});

Any suggestions on how to display this JSON data in HTML using Ionic 2?

["100","300","400"]

Answer №1

Here is a code snippet that demonstrates how to extract values from an array and store them in another array

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  jsonData : any;
  val : any=[];
  constructor(public navCtrl: NavController) {
    this.jsonData=[{"id":1,"label":"saw","total":"100"},{"id":2,"label":"saw1","total":"300"},{"id":3,"label":"saw2","total":"400"}];
    this.jsonData.map((item)=>{
      this.val.push(item.total);
    });
    alert(this.val);
  }

}

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

Utilize a fresh function in Angular to retrieve and store data from a URL into a variable

Currently, I am attempting to utilize Angular in order to retrieve data from a link upon clicking a button. As a newcomer to Angular with only 2 days experience, my knowledge is quite limited. What I aim to achieve is triggering the loading of JSON data w ...

Error occurred in child process while processing the request in TypeScript: Debug Failure encountered

I encountered the following error in TypeScript while running nwb serve-react-demo: Child process failed to handle the request: Error: Debug Failure. Expression is not true. at resolveNamesWithLocalCache (D:\Projects\react-techpulse-components& ...

Encountering a JSON error while attempting to login through an API on the Ionic platform

While implementing the login functionality through API in Ionic, I encountered the following error: Error: Property 'json' does not exist on type '{}'. Below is my loginpage.html code: <ion-header> <ion-navbar> & ...

Tips for retrieving unique records in Linq without duplicates

Hey, I need some help! I have a list of promotion codes and I want to retrieve only the ones that don't have duplicates. For example, from the JSON data below, I want to extract Promotion code A123 and B500 and store them in a separate list. [ { ...

javascript retrieving JSON data through an API request from a redirected URL

I am retrieving JSON data from the Glitch API. Upon opening the link, it redirects to a different domain (the domain was changed from gomix.me to glitch.me) When using Postman for both HTTP requests, I receive identical JSON data. However, there is a d ...

Swift - JSON decoding error: "Was supposed to decode an array of any type, but instead found a dictionary."

Hello there! I have a question here that's at the root of my learning journey. I'm delving into SwiftUI and currently experimenting with data fetching from an API to store it in an array. At the moment, I have two essential files in place: First ...

My loop encountered an 'Undefined' error while attempting to retrieve an object from a JSON file

Hey everyone, I could use some help with a problem I'm having in my for loop. The issue is that I keep getting an "undefined" word in the output of my loop. I am trying to retrieve objects from a JSON file and the loop itself seems to be working corre ...

Limiting the Size of Highcharts Maps with maxWidth or maxHeight

My attempt to adjust the maxWidth of my world map using highcharts isn't altering its size. I have experimented with the following: responsive: { rules: [{ condition: { maxWidth: 500 }, ... As recomme ...

Ways to classify the prop type of a functional component by specifying the different types of props for the FC

I have created two functional components that require different prop types. export interface OrderImageProps { orders: ICartItem[] } const OrderImage: React.FC<OrderImageProps> = (props: OrderImageProps) => { return ( <div> ...

Step-by-step guide on mapping JSON payload values to a Plain Old Java Object in Mule

Looking at the JSON payload provided below: [ { "name": "Car Sale", "id": 1, "status": "processing" }, { "name": "Car Sale", "id": 2, "status": "processing" } ] How can I assign these values ...

Question: What is the best way to store individual user responses in a text document?

With the prospect of returning to school soon, I am working on enhancing my discord bot by implementing a feature that will automatically send each user their zoom link based on the day of the week, hour, and subject. In order to ensure this information is ...

Utilizing Angular2 with Firebase for efficient denormalized data queries

I am currently working on crafting a query for a denormalized database. Drawing inspiration from the example showcased in Firebase's blog post, my objective is to: Retrieve the array of forms associated with the current user and return references to ...

Prisma Date and Time Formatting Challenge

Exploring Nest Js and prisma led me to the need to store DateTime data in my database based on my timezone preferences. joining DateTime @db.Timestamptz(5) ` I found that using @db.Timestamptz resolved my timezone storage issue, but upon retriev ...

Exploring RPG YAJL's ability to parse nested arrays

I am new to RPG and feeling a bit overwhelmed with this task. I have an array called "data" that contains information which I can parse easily. However, within the "data" array, there is another array called "cargoLoaded" that holds a single item named "ca ...

Converting a JAXB XML file into a JSON format

After generating an XML dump file using JAXB and applying multiple transformations, I finally obtain an XML file in the desired format. Now, my goal is to convert this properly escaped and encoded XML file into a JSON file with the help of JAXB. I want to ...

Angular's mechanism for detecting changes in a callback function for updates

I have come across a puzzling scenario regarding a basic issue. The situation involves a simple component with a boolean property that is displayed in the template of the component. When I update this property within a callback function, the property is up ...

Building a DataFrame from JSON data using danfo-js, mongoDB, and mongoose on a Windows 10 platform: A guide

I'm currently facing a challenge in constructing a DataFrame using danfo-js with JSON data retrieved from mongoDB / mongoose. Just to provide some context, the dependencies I am using are danfojs-node: ^0.1.5, mongodb: ^3.3.4, and mongoose: ^5.7.12. ...

Session is not functioning properly as anticipated

import * as express from 'express'; import * as session from 'express-session'; import * as bodyParser from 'body-parser'; const app: express.Express = express(); app.use(bodyParser.json()); app.use(session({ secret: &apos ...

Tips for initializing constructor arguments using JSON during object instantiation in TypeScript

Let's consider a scenario where we have the following class: export class PersonInformation { constructor( public firstName: string = "", public lastName: string = "", public middleName: string = "", ) { } } Now, we&a ...

Exploring the syntax of typescript when using createContext

Just starting out with typescript and I have some questions. Could someone break down the syntax used in this code snippet for me? What is the significance of having two groups containing signIn, signOut, and user here? Is the first group responsible fo ...