Converting JSON data into an array of JSON data in a TypeScript environment: A guide

I received the API response in JSON format as shown below:

{
Company1: 3
Company2: 3
Company3: 3
Company4: 3
Company5: 3
}

My goal is to convert this response into a JSON array in the following structure:

{
  0: {Name:Company1, Index:3},
  1: {Name:Company2, Index:3},
  2: {Name:Company3, Index:3},
  3: {Name:Company4, Index:3},
  4: {Name:Company5, Index:3},
}

Can anyone provide guidance on how to achieve this using typescript? Any suggestions would be greatly appreciated.

Answer №1

const companies = {
  A: 3,
  B: 3,
  C: 3,
  D: 3,
  E: 3
};

const information  = {};

Object.keys(companies).forEach((key, i)=> {
  information[i] = {
    Name: key,
    Index: companies[key]
  }
})

console.log(information);

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

When trying to insert JavaScript into the console, a section of the code fails to execute

In this code snippet, I am fetching the most common English words from Wikipedia, extracting all the words from the page, and then filtering out the commonly used words: // get table data from most common words var arr = []; $.ajax({ url: 'https:/ ...

Unable to properly align divs vertically

Having trouble getting some divs to align properly, whether they are in an accordion or not. The current layout is not what I want: My goal is to have them vertically aligned at the top, despite varying lengths. Any help would be appreciated. I attempted ...

Can you share a method in javascript that extracts href= and title values, similar to PHP's preg_match_all() function?

I received an HTML string as :var code; I am looking to retrieve all the values of href and title similar to how it is done using PHP's preg_match_all(). I have achieved a similar task in PHP with the provided example but now I am curious about how I ...

Tips for handling and storing a large JSON file received in a POST request

Currently, I am in the process of automating various APIs. In certain scenarios, I find myself needing to include extensive JSON messages within the POST request data. These messages can range from 50 to 100 lines and require parameters that need to be ad ...

How to extract the id instead of the value in ReactJs when using Material UI Autocomplete

I am utilizing Material UI Autocomplete to retrieve a list of countries, and my objective is to capture the selected country ID in order to send it back to the database. <Autocomplete options={this.state.countries.map(option => option.name_en + ` ...

Transforming the output of a MySQL query into a JSON format organized like a tree

This question has been asked before, but no one seems to have answered yet. Imagine a scenario where a MySQL query returns results like this: name | id tarun | 1 tarun | 2 tarun | 3 If we were to standardize the JSON encoding, it would l ...

Using the parameter of type 'never' is necessary as per the TypeScript error message, which states that the argument of type 'string' cannot be assigned to it. This error persists even when

https://i.sstatic.net/tkX07.png const index = selectedActivities.value.indexOf(activity_id); I encountered a TypeScript error saying 'Argument of type 'string' is not assignable to parameter of type 'never'. How can I fix this iss ...

Exploring the process of including cube quantities in three.js code

In my current project, I am facing a challenge of incorporating multiple spheres into the scene. Initially, there were only three cubes present, but now I need to include around 10 spheres that are evenly spaced from each other and are rotating at varying ...

Identify the presence of <br /> within a string and envelop the text with <p>

Can a string in JavaScript be parsed to wrap text blocks separated by 2 <br /> tags with opening and closing <p> tags instead of paragraph breaks? Here is an example text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec od ...

Angular 2 template can randomly display elements by shuffling the object of objects

I am working with a collection of objects that have the following structure: https://i.stack.imgur.com/ej63v.png To display all images in my template, I am using Object.keys Within the component: this.objectKeys = Object.keys; In the template: <ul ...

Solving compatibility problems with jquery AJAX requests on multiple browsers

searchCompanyExecutives: function(criteria, callback) { var params = $j.extend({ type: "GET", data: criteria, url: "/wa/rs/company_executives?random=" + Math.floor(Math.random() * (new Date()).getTime() + 1), ...

JavaScript - Relocating the DIV Scrollbar to the Top Upon iFrame SRC Modification

re: I have created a user-friendly website for comparing different potential "expat" cities. Each city has links (target=iframe) for cost of living data, Wikipedia, expat blogs, climate information, and Google Maps. In the left column, there are control ...

Storing the outcome of a query into a variable in Node-RED

I am facing an issue where I need to transfer the value of Object[0] nazwa, which is obtained from a query result (let's say "ab" in this scenario), to another variable. Since I am new to red node and JS, I'm unsure about how to accomplish this t ...

Tips on converting HTML code into a data image URI

My question may seem unconventional, but here is what I am trying to accomplish. I want to create a design similar to the one found at the following link: I would like to embed text with an image and retrieve the data image URL using Ajax. I know how to g ...

mysql2 result inconsistencies

I am fairly new to the world of MySQL and backend development, so please excuse me if this question seems basic. Currently, I am utilizing node/mysql2 for interacting with a MySQL database. The connection is established using a promisified connection pool ...

The functionality of Angular 2's AsyncPipe seems to be limited when working with an Observable

Encountering an Error: EXCEPTION: Unable to find a support object '[object Object]' in [files | async in Images@1:9] Here's the relevant part of the code snippet: <img *ngFor="#file of files | async" [src]="file.path"> Shown is the ...

Searching through the use of autocomplete with alternative parameters

In the example below, - https://codesandbox.io/s/g5ucdj?file=/demo.tsx I am aiming to achieve a functionality where, with an array like this - const top100Films = [ { title: 'The Shawshank Redemption', year: 1994 }, { title: 'The Godfa ...

What is the process for linking a front-end application to a database in HashiCorp Nomad?

Exploring the world of Hashicorp Nomad for the first time and embarking on deploying an employee application. I have the code stored in a Kubernetes yaml file. While I have successfully deployed the frontend application with a Mongo database separately, ...

What is the best way to implement custom JSON serializers for both parent and child classes independently?

Within this example, there exists a primary class called A and a subclass called B: public class A { public final String primaryKey; A(String primaryKey) { this.primaryKey = primaryKey; } } public class B extends A { public final ...

How come the np.shape() function does not output 2-digit tuples for a one-dimensional array?

In my understanding, the np.shape() function returns a tuple containing the number of adjacent elements in each index. For example, when I apply np.shape() to a 2D array: x = np.array(([5,6,2,5],[7,8,9,10])) print(np.shape(x)) I get (2, 4) However, when ...