Generating an array of elements from a massive disorganized object

I am facing a challenge in TypeScript where I need to convert poorly formatted data from an API into an array of objects. The data is currently structured as one large object, which poses a problem.

Here is a snippet of the data:

Your data here...

The goal is to transform this data into an array of objects that group corresponding values together like this:

[
{
KeyBoolean00: "deling",
LabelBoolean00: "Ekstern Deling",
ValueBoolean00: false
},
{
KeyDateTime00: "start",
LabelDateTime00: "Startdato",
ValueDateTime00: "2020-01-07T00:00:00Z"
},
{
KeyString00: "status"
LabelString00: "Prosjektstatus"
ValueString00: "Godkjennt"
}
]

I have attempted some solutions but haven't found the right approach yet.

Answer №1

Utilize the substring method to extract key numbers as 00, 01, ... and employ the reduce function to construct the required object

const obj = {
    KeyBinary00: null,
    KeyBinary01: null,
    KeyBinary02: null,
    // Remaining keys omitted for brevity
};

const result = Object.values(
    Object.keys(obj).reduce((acc, key) => {
        const number = key.substring(key.length - 2, key.length); // 00

        acc[number] = acc[number] ? { ...acc[number], [key]: obj[key] } : { [key]: obj[key] };

        return acc;
    }, {}),
);

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

Transform a two-dimensional array into a JSON object within the Laravel framework

Recently, I've been working on converting a two-dimensional array into a JSON object. Here is the current output produced by my controller: [ [ { "id": 1, "title": "V1", &q ...

Ways to extract values from a JSON output using comparisons

I am dealing with a JSON data structure that contains information about various teams. My task is to compare the values in the teams array with the values stored in html_content.position[i]. In this comparison, the index i needs to be dynamically set withi ...

The Angular router seems to be refusing to show my component

My Angular 2 App includes a Module called InformationPagesModule that contains two lazy load components (Info1 Component and Info2 Component). I would like these components to load when accessing the following routes in the browser: http://localhost:4200/ ...

Resolve the error message "Class is not a constructor" which arises while utilizing namespaces in TypeScript with WebPack

Issue with TypeScript Namespaces in PIXI.js As I delve into the world of TypeScript and PIXI.js without any framework, I find myself facing a challenge. Previously, at work, we utilized namespaces with the module keyword for a cleaner structure. However, ...

Cannot find property '' within type 'never'. (Using TypeScript useState with mapped JSON data from a server)

Currently, I am following a tutorial on using React with Material-UI and decided to add TypeScript into the mix. If you're interested, here is the link: Net ninja MUI The file Notes.tsx contains the code for fetching data from a JSON server and then ...

What is the process of including a String Array within a JSON object?

I am currently working on creating a JSON object in an Android application. Specifically, I am facing difficulties in including a string array within the object. A = { "class" : "4" , "name" : ["john", "mat", "jason", "matthew"] } Below is th ...

I prefer not to run the next.js SWR until after the initial rendering

Development Setup ・ next.js ・ typescript ・ swr This uses swr for communication purposes. I am looking to only trigger it when the query value changes. However, it is also being executed during the initial rendering. How can I prevent it ...

Tips for refining TypeScript discriminated unions by using discriminators that are only partially known?

Currently in the process of developing a React hook to abstract state for different features sharing common function arguments, while also having specific feature-related arguments that should be required or disallowed based on the enabled features. The ho ...

What is the best method to adjust the width of the PrimeNG ConfirmDialog widget from a logic perspective

Currently utilizing "primeng": "^11.2.0" and implementing the ConfirmDialog code below this.confirm.confirm({ header: 'Announcement', message: this.userCompany.announcement.promptMsg, acceptLabel: this.userCompany.announcement ...

Removing data from a table using an identifier in Typescript

Recently, I have made the switch from using Javascript to TypeScript. However, I am facing an issue while trying to delete data from a table in my code. Whenever I attempt to delete data, I encounter this error message: Property 'id' does not e ...

What is the best way to access automatically generated JavaScript variables in TypeScript?

I am currently facing an issue with integrating a Google login API in my React project and I need some help. The problem arises when the user already has an active session, rendering the API unnecessary. The Javascript solution provided is as follows: co ...

Using React's Ref to handle conditional rendering and handling the case when

I am facing an issue with my React ref animationRef being null when I conditionally render an element. It works perfectly fine outside of the condition, but not within it. Is there a way to ensure that the ref has a value even when conditionally renderin ...

What is the best way to guarantee an Array filled with Strings?

Which is the proper way to define a potentially array of strings? Promise<Array<string>> Or Promise<string[]> ...

Eliminate null objects from a JSON array with the help of GSON

{ "ChangeRequests": [ {} ] } Utilize Gson to remove the empty model from the JSON array. To achieve this, add a new model inside the list where all values are set to null using Gson. data class TestRequest( @SerializedName("ChangeRequests") val ...

I don't understand why I'm receiving the error message "Unsafe assignment of an `any` value" when I clearly defined the value as a string with a default value

I am puzzled by the 2 eslint errors in this code snippet. The property is declared as a string with a default value: export default { name: '...', props: { x: { type: String, required: true, default: '' } ...

Performance of basic arithmetic operations on small arrays using NumPy

It's uncertain whether there is a solution for this question other than accepting the overhead and moving forward, but there's always a possibility. I'm performing simple mathematical operations on small 1D NumPy arrays (6 to 10 elements) w ...

managing arrays in JavaScript with multiple tables

**I have successfully written code for calculating the sum of inputs in one table** $('#example122').on('input', '.calculate', function () { calculateTotal(); }); function calculateTotal() { var grandTotal = 0; $ ...

No errors are being displayed with the React Hook Form using Zod and Material UI

Presenting my custom ProductInfoForm built using Material UI, react-hook-form with zod validations. However, I am encountering an issue: upon submitting the form, the data is displayed in the console as expected, but when intentionally triggering an error, ...

What is the best way to synchronize API definitions between the server and client using TypeScript?

My setup involves a server (TypeScript, NestJS) and a client (TypeScript, Angular) that communicate with each other. Right now, I have the API response DTO classes defined in both the server to output data and in the client to decode the responses into a ...

Difficulties encountered when trying to load a URL or API that contains an array in javascript/p5js

I've run into a coding issue that I'm trying to troubleshoot in p5js. My goal is to retrieve data from a URL/API. The line `kursMin[1]...` gives the correct stock value, but the line `kursMin[i]` returns `[object Object]2021-03-124. close` Bo ...