Converting an object into a list of lists using Typescript

When making an API call from Angular 5, the response is returned in the following format.

{   "metadata":{
      "lastSyncTime":"2000-11-21T16:07:53",
      "dataFromDB":true
   },
   "allocationReports":[      {
         "allocatedUserCount":100,
         "healthGoalName":"Eat Healthier"
      },
      {
         "allocatedUserCount":130,
         "healthGoalName":"Feel Happier"
      },
  
      {
         "allocatedUserCount":150,
         "healthGoalName":"Quit Smoking"
      }
   ],
   "overall":{
      "usersWithGoalCount":0,
      "registeredCount":500,
      "eligibleCount":280
   }
}

In order to plot multiple donut charts using this data, it needs to be transformed into a list of lists (or Array of Arrays). Various methods have been attempted such as utilizing .map, however all values are being retrieved in a single list. How can the data be structured in the desired format below.

The required format is :

[ 
 [
  { "x": "Eat Healthier", "y": 100 },
  { "x": "Total registered", "y": 500 } 
 ],
 [
  { "x": "Feel Happier", "y": 130 },
  { "x": "Total registered", "y": 500 } 
 ],
 [
  { "x": "Quit Smoking", "y": 150 },
  { "x": "Total registered", "y": 500 } 
 ]
]

The total registered value will be sourced from overall.registeredCount

Answer №1

give this a shot

object={"info":{ "lastSyncTime":"2000-11-21T16:07:53", "dataFromDB":true }, "allocationReports":[      { "allocatedUserCount":100, "healthGoalName":"Eat Healthier" }, { "allocatedUserCount":130, "healthGoalName":"Feel Happier" }, { "allocatedUserCount":150, "healthGoalName":"Quit Smoking" } ], "overall":{ "usersWithGoalCount":0, "registeredCount":500, "eligibleCount":280 } }

result=object.allocationReports.map(item=>{return [{name:item.healthGoalName,count:item.allocatedUserCount},{name: "Total registered users", count: object.overall.registeredCount }]})

console.log(result)

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

NodeJS guide: Enabling cross-domain access for web services

Currently, I am developing a nodejs server and facing the challenge of needing to access additional services through ajax from a different domain. Can anyone provide guidance on how to bypass the cross-domain restriction within nodejs code? Please note th ...

What is causing JS to malfunction and preventing App Scripts from running `doGet()` when using either `e` or `event` as parameters?

Following a Basic Web App video last night, I meticulously followed every step until the very end where things started to go wrong. Today, I decided to start from scratch and recreate it all. Despite weeks of coding practice, I can't seem to figure ou ...

Easy pagination for angular's in-memory-web-api

Looking for help to implement pagination in Angular-in-memory-web-api. Currently, I have the following setup: import { InMemoryDbService } from 'angular-in-memory-web-api'; export class InMemoryDataService implements InMemoryDbService { ...

Investigating the memory leak caused by jQuery/Sizzle when checking the

During the debugging process of my application with the 'Profiles' feature in DevTools, I noticed an accumulation of "Detached DOM tree's". These detached nodes contain a retaining tree primarily made up of checkContext functions (which orig ...

Rails link_to using remote true property not refreshing the view

I'm currently working on an app that features events. On the index view for events, I loop through each event to display relevant data. Within this loop, there is a link to 'Upvote' a specific event which triggers an ajax request. However, I ...

Retrieve the data from every dropdown menu

How can I retrieve the selected values from all created selects when a button is clicked? I have attempted using both refs and v-model, but neither of them are functioning as expected. <div v-for="attribute in attributes" class="col"> {{ a ...

Using jQuery, is it possible to retrieve the product name and image dynamically?

I'm currently working on implementing an add to cart functionality using jQuery. When the add to cart button is clicked, the product name and image should be displayed. I can achieve this statically but I need help figuring out how to dynamically retr ...

Issue with boolean data binding in Angular dialog button causing unresponsive behavior

My issue seems simple, but despite my efforts and searches online, I haven't been able to find a solution. In my Angular component, I have 2 dialogs and a button bound to a boolean in my typescript as shown below: export class ModalDB { constructor ...

What is the process for transferring an environment.json file to the output directory and then utilizing it with Webpack 4?

Our application is expanding with multiple environments and vendors on the horizon. While the traditional approach of running webpack --env.NODE_ENV=myenvironment works for now, it will soon become inefficient. The main objective here is to streamline the ...

Chrome Extension: Despite adding it to web_accessible_resources, the resource remains unavailable

I'm currently developing a Chrome Extension with React/Redux and utilizing Webpack. As part of the optimization process, I have started migrating resources to a separate file using the WebPack DLLReference plugin. After generating the dll/dll.vendor. ...

Is it possible to align a div on the same line with an h1 header that spans multiple lines using CSS?

I am currently working on the following code: <h1><span>Test Heading This is the text that I want to display on the right side. This is the text that I want to display on the right side. This is the text that I want</span></h1> < ...

Is it possible to update a Rails element using an AJAX request?

I've delved into a plethora of information regarding Rails, AJAX, and 5.1 Unobtrusive Javascript. It provides insight on how to handle AJAX calls in Rails with a .js file, for example. However, my goal isn't to serve up an entire .js file; rathe ...

The use of async components in Vue.js with named imports

I understand how to dynamically load components asynchronously in Vue. For example, instead of importing MyComponent directly: import MyComponent from '@/components/MyComponent' export default { components: { MyComponent } } We can use ...

Displaying data from alternate indices in two separate columns with the use of v-for

My goal is to display the even indexes of the array myArray in a unique way: myArray: [{'label': 'hasan', 'value': 'hosein'}, {'label': '1', 'value': '2'}, ...

Determining the presence of generic K within generic M in Typescript Generics and Redux

Hello there I am currently working on minimizing repetitive code in my react application by utilizing Redux state. After choosing the Redux structure to use (refer to Context), I now aim to make it more concise. To achieve this, I have developed a generic ...

What is the scope parameter for the facebook-node-sdk in node.js?

https://github.com/amachang/facebook-node-sdk I decided to utilize this module in order to create a Facebook-integrated login for my node.js project, following the example provided with express: var express = require('express'); var Facebook = ...

What is the solution to resolving a Typescript error within an imported library?

I've encountered a Typescript error that I'm unable to resolve because it doesn't originate from my code. The issue arises when I import a NextJs/React module named next-seo and use it as instructed: <NextSeo config={seoConfig} /> The ...

ERROR: The request for URL /Angular2 resulted in an HTTP status of 404, indicating that the resource could

Trying to receive an http response within my service: import { Injectable } from '@angular/core'; import { Http, Response } from '@angular/http'; import 'rxjs/add/operator/map'; @Injectable() export class CartService { ...

Controlled Material-UI v5 DateTimePicker triggers input focus upon closure

Is there a way to have 2 DateTimePicker components as siblings, and when I click on the second one while the first one is still open, it should open a new DateTimePicker with focus on it? Can someone help me achieve this? Link to code example I want the ...

Issue with material datepicker not initializing start value when input field is clicked

Using the Material date picker, I've implemented the date picker feature with the startAt binding to establish a default selected value. Users can open the calendar overlay by clicking on the input field, thanks to the (click) and (focus) event bindin ...