Using TypeScript to interpret JSON - insert a 'data' label

Consider the following example of a JSON structure:

[  
   {  
      "id":1,
      "position":3,
      "articleNumber":"ServiceElement"
   },
   {  
      "id":2,
      "position":2,
      "articleNumber":"ServiceElement"
   }
]

Is there a way to transform it into the desired format below?

{  
   "data":[  
      {  
         "data":{  
            "id":1,
            "position":3,
            "articleNumber":"ServiceElement"
         }
      },
      {  
         "data":{  
            "id":2,
            "position":2,
            "articleNumber":"ServiceElement"
         }
      }
   ]
}

The addition of the data tag is necessary for identifying objects in order to implement a TreeTable. The original JSON structure does not meet the required format for this purpose.

Answer №1

To transform the shape of items, simply utilize the map function.

Initially, we create a new object { data: } at the first level and then assign the result of array.map to the data property.

const array = [  
   {  
      "id":1,
      "position":3,
      "articleNumber":"ServiceElement"
   },
   {  
      "id":2,
      "position":2,
      "articleNumber":"ServiceElement"
   }
];

const mapped = { data: array.map(item => ({ data: item }))};
console.log(mapped);

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

Issue with hydration when logging in with Next-Auth in NextJS 13.4

Step-by-step Instructions: 'node -v' -> v18.16.1 'npx -v' -> 9.8.0 To start, I created a new Next.js app by running npx create-next-app@latest in the terminal within my app folder. Here is a link to the package.json file. Nex ...

Error: Unable to access the 'nom_gr' property of null - encountered in Chrome

<ion-col col-9 class="sildes"> <ion-slides slidesPerView="{{nbPerPage}}" spaceBetween="5"> <ion-slide *ngFor="let slide of lesClassrooms; let i = index" (click)="saveCurrentSlide(i)"> ...

Constructing a Primeng MessageService causes a blank webpage to appear

After downloading the QuickStart CLI of PrimeNG for Angular, I added a second component for a chart that was already included in the UI components. Everything seemed to be set up correctly, but when I saved, I ended up with a completely blank page for the ...

Utilizing the ABP framework for implementing pagination in ngx datatable

Is there a way to display pagination footer in ngx datatable within the abp framework for Angular? I am currently using ListService with PagedResultDto. Do I need to implement pagination externally? <ngx-datatable [scrollbarH]="false&quo ...

Connection drops unexpectedly on Azure App Service after deploying SignalR

I'm currently facing an issue with my Angular app that utilizes SignalR and .NET core in the backend. Surprisingly, everything works perfectly fine when running locally, but as soon as I deploy it to Azure App Service, the frontend establishes a conne ...

What sets apart a class from a service in NativeScript?

I am embarking on the journey of learning Nativescript + Angular2, and while reading through the tutorial, I came across this interesting snippet: We’ll build this functionality as an Angular service, which is Angular’s mechanism for reusable classes ...

Attempting to deploy my Angular project on Heroku but encountering an error

While attempting to deploy my Angular project to Heroku, I followed all the necessary steps but encountered an error stating that the application failed to serve the page. As the application owner, it is advised to check the logs for more details. This can ...

Obtaining JSON information with CakePHP

Is there a way to extract JSON data from the following URL: I am currently working with cakephp 1.3 and looking for solutions on how to accomplish this task. ...

What could be causing the class methods in my NestJS DI to be stored as undefined values in a map?

I came up with an EventService to manage custom events that are added through a special decorator. Everything appears to be functioning correctly, but the issue arises when attempting to access the methods within the stored event class as they return undef ...

The class 'AngularFireList<{}>' cannot be assigned to the type 'FirebaseListObservable<any>'

Currently, I am developing a mobile app using Ionic 3, and I am facing an issue while trying to establish a connection with the Firebase database. The error message that keeps popping up is: Type 'AngularFireList<{}>' is not assignable to ...

Special JSON character scraping using PromTail

My current setup involves using a JSON stage for my PromTail scrape config. The log I'm working with is formatted as follows: { "@l": "info", "foo": "bar" } My goal is to extract the @l property using th ...

Ways to fix npm ERR! code ECONNRESET during Angular cli installation

I have successfully set up nodejs on my Windows-10 PC. Upon running: npm version, I received the following output: C:\WINDOWS\system32>npm version { npm: '8.5.3', node: '16.14.0', v8: '9.4.146.24-node.20', ...

Automatically Submitting React Forms Using Code

Hey there! I'm having some trouble getting my form to submit inside the handleSubmit function in React. I need to prefetch some information before submitting the form, but I can't seem to trigger the submission once I'm done with my operatio ...

Having trouble locating the .ts module when executing a Node script with experimental modules enabled

I am currently developing a node express project and I need to run a node script from the terminal. Within my project, there are some .ts files that I want to include in the script (MyScript.js). Below is the content of MyScript.js: import get from &apos ...

When utilizing Angular 2, this message is triggered when a function is invoked from the Observable

One of my services is set up like this: @Injectable() export class DataService { constructor(protected url: string) { } private handleError(error: Response) { console.log(this.url); return Observable.throw(new AppError(error)); ...

Angular MatDialog failing to display the gray overlay background

I am currently working with Angular 6 and have successfully integrated the theme into my project. The code snippet below shows how I open the dialog. constructor(private dialogo: MatDialog) { } ngOnInit() { this.dialogo.open(Carrega ...

Encountering the error "tsx is not defined" during a Jest test in a React/TypeScript project

I'm currently working on implementing Jest tests within a React project that has enforced TypeScript settings. In a simple test.tsx file located in the test folder, I have the following code: import React from 'react'; describe('Test& ...

Retrieve JSON data from a WordPress site that is powered by WooCommerce, using basic authentication

I am in the process of creating an Android application that retrieves product data from a WordPress site. The website has the Woocommerce plugin installed. I have been able to successfully obtain the JSON data using Postman software and basic authenticat ...

What could be the reason for the custom component app-transition-group and app-transition-group-item not functioning properly in Angular 8, even though it was working fine

I developed a unique Angular8 custom component that rearranges list items. However, I keep encountering the following error: Can't bind to 'app-transition-group' since it isn't a known property of 'ul'. Do you have any sugge ...

Utilizing C# to Decode Nested HTML Elements from JSON

I recently came across a very helpful JQuery function that allows me to serialize nested elements. However, I am facing an issue when trying to deserialize it using c#. Upon running the code, I encountered the error message: No parameterless constructor ...