What is the best way to transform an array of different sizes into a JSON object?

Looking at the object array below, it's clear that the size and members of the array can vary. So, how can I efficiently convert all this data into a JSON object?

Array:
[0] =>{ "Name": XYZ }
[1] =>{ "Gender":M }
[2] =>{ "DOB":09085672 }
[3] =>{ "City":London }
[4] =>{ "State":UK}
[5] =>{ "County":Chester }

JSON Output:
{
"Name":XYZ,
"Gender": M,
"DOB" : 09085672,
"City":London,
"State": UK,
"County":Chester
}

Answer №1

If you want to combine multiple objects in an array into a single object, one way to do it is by utilizing the spread syntax and Object.assign() method:

const arr = [
  { "Name": 'John' },
  { "Age": 25 },
  { "City": 'New York' }
]

const result = Object.assign({}, ...arr)

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

Using Typescript to Import One Namespace into Another Namespace

Is it possible to export a namespace from one typescript .d.ts file and then import that namespace into another .d.ts file where it is utilized inside of a namespace? For instance: namespace_export.d.ts export namespace Foo { interface foo { ...

I'm having trouble getting my innerHTML command to update anything on the webpage, and the reason is eluding me

Below is the code snippet provided: <div id="js"><button onclick="document.getElementById('js').innerHTML=('<form> <input type=text name=tick1></input> <input type=text name=tick2></input> ...

Jquery append functionality failing to set value in select element

Recently, I've been working on an MVC project that utilizes PHP, JavaScript, and an Oracle database on the backend. However, I've encountered a perplexing issue that I just can't seem to solve. I have a select element that I'm trying to ...

selecting a style with "display: block;" using Selenium WebDriver

I'm having trouble clicking on an element with the style "display: block;" and can't seem to make it work. Here is the sample html snippet <div class="fl f18 dtoggler pointer underline some-padding new_data_entry" data-div-id="eBWJcg" data-d ...

Transforming a pointer from a union to a pointer to the specific element type of an array member within the union

I have a union: union Obj64 { uint64_t u64; uint32_t u32[2]; uint16_t u16[4]; uint8_t u8[8]; }; static_assert(sizeof(union Obj64) == sizeof(uint64_t), "Unexpected trailing padding in union!"); Now I possess a pointer to the union ...

Tips for showcasing a dataset within a table using Angular.js ng-repeat

I am encountering an issue where I have to present an array of data within a table using Angular.js. Below is an explanation of my code. Table: <table class="table table-bordered table-striped table-hover" id="dataTable" > <tbody> ...

What is the best way to design a table to allow for changing the colors of specific boxes in the table when clicked?

I am looking to create a feature where the elements in this table change color from the background color to red and back to the default color when they are clicked. Can anyone help me achieve this? <table align="center" style="height: 355px;" width=" ...

React-query: When looping through useMutation, only the data from the last request can be accessed

Iterating over an array and applying a mutation to each element array?.forEach((item, index) => { mutate( { ...item }, { onSuccess: ({ id }) => { console.log(id) }, } ); }); The n ...

Dividing the ZMQ socket from the express app.js by integrating with mongodb

Currently, I am working on an Express application that is responsible for gathering data sent from a remote machine through ZMQ and then updating a MongoDB database with this information. The data updates are transmitted every 5 minutes in the form of enc ...

I'm curious about something as a beginner. Can you explain what **.config.js and **.vue.js files are? How do they differ from regular .js files

I have a simple question that I haven't been able to find the answer to through searching. I've come across files like vue.config.js generated by vue-cli, as well as files like Content.vue.js. At first, I assumed that after the period was the fi ...

How to Execute Embedded JavaScript Using Python's Selenium?

Looking to automate a process with Python script using a web interface. After installing Selenium, I can successfully access the web interface and log in. However, I am unable to locate certain buttons as they are embedded within .js files. <script src= ...

Using jQuery, extract the input value and verify its accuracy. If correct, display the number of accurately predicted results

I am attempting to extract the value from an input field and validate if the answer is accurate. Rather than simply indicating correctness, I aim to analyze and display the number of correct responses alongside incorrect ones. Encountering difficulties wit ...

The Gusser Game does not refresh the page upon reaching Game Over

Hi there, I am a beginner in the world of JavaScript and currently working on developing a small guessing game app. However, I have encountered an issue where the page is not reloading after the game is over and the 'Play Again' button appears to ...

Merging Technology: Integrating Maps into Hybrid Applications

Currently, I am developing a mobile application using React-Native with a main focus on: Map Integration: Partial Success - I have successfully implemented all features mentioned in this link. The remaining task is to display live routing based on curren ...

Creating a straightforward HTML output using jQuery and JSON

Currently, I am teaching myself how to handle data flow from mySQL to PHP to JSON/JavaScript, but I seem to be stuck at this particular stage. My goal is to loop through external JSON data and display it in an unordered list. Despite attempting to modify t ...

Creating a layered image by drawing a shape over a photo in Ionic using canvas

While there are plenty of examples demonstrating how to draw on a canvas, my specific problem involves loading a photo into memory, adding a shape to exact coordinates over the photo, and then drawing/scaling the photo onto a canvas. I'm unsure of whe ...

Mastering React hooks: A guide to effectively updating and rendering elements

Each time I click the delete button, it seems to only remove the last element instead of the specific one based on index. Is there a better way to achieve this without changing from <input defaultValue={name} /> to <input value={name} /> in t ...

Issue with React JS routing: encountering an error message stating "Uncaught TypeError: Super expression must either be null or a function, not object"

Recently, I delved into building a basic web application with React JS. Despite my brief experience with React JS, I ventured into exploring the routing aspect following an online tutorial. However, my code seems to be malfunctioning. Presented below is m ...

Error discovered in Webpack syntax

I'm a newcomer to webpack and feeling lost on how to properly configure the settings. Here is my project structure: / -- /public -- /js -- app.js -- /css -- app.scss -- /node_modules -- /autoprefixer -- /babel-loader ...

Encode image into base64 format without the need for file uploads

Is there a way to save an image in localStorage in base64 format without uploading it? I want to convert an existing image into base64. Can someone provide guidance on how to achieve this? function loadImageFileAsURL() { var filesSelected = document ...