convert a JSON object into an array field

I am looking to convert a list of objects with the following structure:

{
    "idActivite": 1,
    "nomActivite": "Accueil des participants autour d’un café viennoiseries",
    "descriptionActivite": "",
    "lieuActivite": "",
    "typeActivite": "",
    "horaireDebActivite": 1512028800,
    "horaireFinActivite": 1512059388,
    "horaireDebSession": 1512030600,
    "horaireFinSession": 1512318588,
    "nomSession": "",
    "isInSession": false
}

to another format like this:

[
    "idActivite": 1,
    "nomActivite": "Accueil des participants autour d’un café viennoiseries",
    "descriptionActivite": "",
    "lieuActivite": "",
    "typeActivite": "",
    "horaireDebActivite": 1512028800,
    "horaireFinActivite": 1512059388,
    "horaireDebSession": 1512030600,
    "horaireFinSession": 1512318588,
    "nomSession": "",
    "isInSession": false
]

using TypeScript 2.5.

Answer №1

Although both concepts may seem similar, it is important to note that they are fundamentally different in nature. An array is a collection of elements accessed by numerical index, while the structure described in your example utilizes string-based indexes and thus falls under the category of objects according to the JSON specification. If you encounter difficulties iterating through such a structure, consider using the following code snippet:

for(var key in myObject){
  console.log(myObject[key]);
}

Answer №2

If you're looking to create an array in the format [ {key, value} ], you can use the following approach:

let exampleObject = { first: 1, second: 2 }
let newArray = []

for (const property in exampleObject) {
    newArray.push({ key: property, value: exampleObject[property] })
}

console.log(newArray)

This method maintains a linear scaling relationship with the size of the array being processed.

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

A lambda function that requires more than 3 seconds to execute along with a warmup time of 5-10 seconds before each execution

My node.js function consists of 2 REST API calls and a socket connection output, hosted in an AWS lambda. It has a warm-up time of 5-10 seconds and an execution time of over 3 seconds. Running the code locally completes both requests, socket connection, a ...

Convert JSON data into jQuery format, then present it in a list format using `<ul>` and

After making an ajax request, I receive a JSON string containing error messages regarding required form fields: first name, last name, email, usercode, password, password confirmation, and captcha. { "first_name":["The first name field is required."], " ...

Show information from mysql server in a listView with the help of a fragment

Currently in the process of developing my very first app, which includes a navigation drawer feature. The main challenge I am facing is how to retrieve data from a MySQL database (running on XAMPP) and display it in a ListView. Despite researching numero ...

Converting a JavaScript string into an array or dictionary

Is there a way to transform the following string: "{u'value': {u'username': u'testeuser', u'status': 1, u'firstName': u'a', u'lastName': u'a', u'gender': u'a&a ...

Divide an array into smaller chunks using array_chunk dynamically, depending on the elements in

Is there a way to split an array dynamically, similar to the function array_chunk, but instead of specifying the size as an integer for the second parameter, can we use an array like different_sizes to define the chunk sizes? $input_sub_arr = range( ...

Combining two JSON objects into a single JSON object using Python Flask

I currently have a Python Flask 3.4 web service running. In addition, I am using a MySQL database that consists of two tables - Table 1 and Table 2. For Table 1, I am able to fetch data in JSON format and display it on the browser. As for Table 2, I hav ...

Handling Click and Mouse Events with React [react-sortable-hoc, material-ui, react-virtualized]

I have come across an interesting example that I would like to share with you. Check out this live working example on Stackblitz When the delete button on the red bin icon is pressed, the onClick event handler does not get triggered (sorting happens inst ...

Tips for adjusting a Bootstrap table to the size of the page it's on

app.component.html <html> <nav class="navbar navbar-expand-md"> <div class="container"> </div> <div class="mx-auto order-0"> <a class="navbar-brand mx-auto" href="#">GURUKULAM PRE-SCHOO ...

Utilizing TypeScript to reference keys from one interface within another interface

I have two different interfaces with optional keys, Obj1 and Obj2, each having unique values: interface Obj1 { a?: string b?: string c?: number } interface Obj2 { a: boolean b: string c: number } In my scenario, Obj1 is used as an argument ...

A new node.js package that offers a customizable method for comparing json data

Currently, I am in the process of developing a testing suite for our REST API within node.js. My primary query involves finding a module that is capable of performing JSON comparison in a customizable manner. For instance: { "id":"456", "data":"Test_Data" ...

Tips for continuously running a loop function until retrieving a value from an API within a cypress project

Need help looping a function to retrieve the value from an API in my Cypress project. The goal is to call the API multiple times until we receive the desired value. let otpValue = ''; const loopFunc = () => { cy.request({ method: &ap ...

Custom options titled MUI Palette - The property 'primary' is not found in the 'TypeBackground' type

I am currently working on expanding the MUI palette to include my own custom properties. Here is the code I have been using: declare module '@mui/material/styles' { interface Palette { border: Palette['primary'] background: Pa ...

ASP.NET not functioning correctly in returning JSON errors

Currently, I am attempting to test an error in Json notation by making an ajax call. Below is my server method for this task: [System.Web.Services.WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static string publishError() { ...

Transforming Observable into a Promise

Is it considered a best practice to convert an observable object to a promise, given that observables can be used in most scenarios instead of promises? I recently started learning Angular and came across the following code snippet in a new project using ...

Displaying text and concealing image when hovering over a column in an angular2 table

I am currently using a table to showcase a series of items for my data. Each data entry includes an action column with images. I would like to implement a feature where hovering over an image hides the image and reveals text, and vice versa (showing the im ...

Tips for arranging backend data horizontally within Bootstrap horizontal cards

After setting up my Angular application, I created a dashboard page and made API calls for dynamic signals (signal-1, signal-2, etc). To showcase this data, I decided to use Bootstrap horizontal cards. However, I'm facing an issue with displaying the ...

Send information to the main application component

Can data be passed two or more levels up from a Child Component to the App Component? https://i.stack.imgur.com/JMCBR.png ...

Hovering over the Star Rating component will cause all previous stars to be filled

I'm in the process of creating a Star Rating component for our website using Angular 11. I've managed to render the 5 stars based on the current rating value, but I'm having trouble getting the hover styling just right. Basically, if I have ...

restructure the presentation of hierarchical data, organized around a specific element

Below is a JSON with hierarchical data that needs to be converted from a flat structure into a parent-child JSON format: [{ "ID": 1042, "NameID": "200", "Name": "related", "path": "1042" }, { "ID": 1561, "NameID": " 230", "Nam ...

Why use rxjs observables if they don't respond to updates?

I have an array of items that I turn into an observable using the of function. I create the observable before populating the array. However, when the array is finally populated, the callback provided to subscribe does not execute. As far as I know, th ...