Convert the JSON data into the specified format using JavaScript

Is there a way to transform JSON data like this in JavaScript?

data = [{acquired: "2018-03-09T22:49:52.935Z", mean_ndvi: -0.0483685}
{acquired: "2018-02-13T22:49:16.568Z", mean_ndvi: 0.00595065}
{acquired: "2018-04-01T22:50:30.912Z", mean_ndvi: -0.033455}]

into this precise format?

data = {"2018-03-09T22:49:52.935Z":-0.0483685, "2018-02-13T22:49:16.568Z": 0.00595065, "2018-04-01T22:50:30.912Z": -0.033455}

This is what I've tried so far:

var json =data; var obj2 ={}; var obj3 =[]; 
for(var i = 0; i < json.length; i++) 
{ var obj = json[i]; 
obj2 = {[obj.acquired]:obj.mean_ndvi};
obj3.push(obj2); } 
console.log(obj3);

I am currently getting the output in separate lines like this:

 {2018-04-01T22:50:30.912Z: -0.033455}, 
 {2018-04-01T22:50:30.912Z: -0.033455}, 
 {2018-04-01T22:50:30.912Z: -0.033455}

Any suggestions on how to modify it to display the result in one row?

Answer №1

To implement this logic using the reduce() method, you can utilize destructuring and spreading in the following way:

data.reduce((a, {acquired, mean_ndvi}) => ({[acquired]: mean_ndvi, ...a}), {});

Here is the full code snippet for reference:

const data = [
    {acquired: "2018-03-09T22:49:52.935Z", mean_ndvi: -0.0483685},
    {acquired: "2018-02-13T22:49:16.568Z", mean_ndvi: 0.00595065},
    {acquired: "2018-04-01T22:50:30.912Z", mean_ndvi: -0.033455}
];

const result = data.reduce((a, {acquired, mean_ndvi}) => ({[acquired]: mean_ndvi, ...a}), {});

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

Only the initial value is being processed in the for loop using Javascript

I am having issues trying to sum the values within a for loop sourced from my Django database using JavaScript. Currently, when I iterate through the loop, only the first value in the array is returned. I need assistance in finding a solution that allows ...

What is the method for inserting a line break into a string that is being transferred to a component in Next JS?

I am currently collaborating on a group project that involves developing a website. One of my team members created a ContentBlock component to facilitate the creation of new pages (see component code below). I have been working on a new page for the site a ...

Automatically scroll to the end of the data retrieved through ajax

I am currently developing a live chat website for my users and I am using ajax to fetch the chat messages. However, I am facing an issue where whenever I enter a new message, it does get added to the database and displayed, but the scroll doesn't auto ...

Pass for Achieving the Bokeh2 Effect

I'm attempting to transfer the Bokeh2 Depth of Field effect to an effect composer pass. Every time I try to run it, I encounter the following error: glDrawElements: Source and destination textures of the draw are the same. Below is the render functi ...

Which is the better choice: JSON file or SQL for storing data?

I am in the process of developing a website that will feature an ajax search functionality. This search will retrieve data either from a JSON file or from a MySQL database. I am unsure which technology would be best suited to store this data. With approxim ...

Update the directory path for Angular ui-bootstrap templates

Currently, I am attempting to utilize ui-bootstrap.min.js in conjunction with external templates. An issue has arisen where the error message reads as follows: http://localhost:13132/Place/template/timepicker/timepicker.html 404 (Not Found) I desire fo ...

Designs for an HTML5 Cheeseburger navigation interface

I've been struggling to implement a functional and visually appealing hamburger menu on my website. The challenge lies in integrating the menu seamlessly into the page rather than having it just pop up abruptly. Despite trying various webkit tools, I ...

Adjust the field of view of the camera in ThreeJS

I'm currently working on adjusting the field of vision for a camera without having to create a new one. So far, I've successfully achieved this for the position using the following code: camera.position.set() Now, I'd like to implement a s ...

Converting Persian calendar date to Gregorian in JavaScript

Looking for assistance in converting a date from 1379/10/02 to AD format, specifically to the date 2000/4/29 using the momentJs library. Any help with this task would be greatly appreciated. Thank you! ...

Looking to reload a div with a captcha in a contact form without the need to refresh the entire page

I have integrated a div tag into my contact form to contain the captcha elements. If the user is unable to read the captcha image, I want to provide them with an option to refresh that specific section without reloading the entire page. I have tried variou ...

Leveraging Mermaid for angular applications

As a newcomer to Mermaid, I am attempting to integrate it into my Angular project. Placing it in my HTML has proven successful. <script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.0.1/mermaid.min.js"></script> <div class="merma ...

ARM Error: The specified Template Resource could not be located through the resource() function with the usage of copyIndex

In my current endeavor, I am attempting to conditionally assign resource property values by translating runtime resource properties within a copyIndex loop. After deploying the ARM template below, I encountered the following error: An issue arose while ...

jQuery function duplicates row in table

I am trying to utilize jQuery to generate a table, but I seem to be encountering an issue with my code: function showGrid(url, container, columns, page) { jQuery(container).empty(); var tr = jQuery("<tr class=\"mobileGridHeader\"> ...

Enhancing Plotly Graphs with Custom Node Values

Encountering an issue while attempting to assign values to nodes on a plotly graph. Code: <html> <head> <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> <style> .graph-container { ...

The dialog box in my ajax jquery MVC2 asp.net application is refusing to open

The delete dialog box does not open with my code: view: `<div id="dialog-confirm" title="Delete dialog" style="display: none;"> <p> Are you sure you want to delete the point? </p> </div>` Javascript: `option ...

Different methods for storing JSON in DynamoDB

I am currently working on a project that requires integration with an external application. This external application produces lengthy and varied JSON data for each request. My goal is to store this JSON in DynamoDB. What would be the most effective appro ...

Use jQuery to display the user input value in real-time

I am in the process of developing a dynamic calculation program to sharpen my jQuery skills, but unfortunately, I'm facing some challenges. I have managed to store values in variables as shown in the code snippet below. <form> Table of: ...

Cloning a repository does not support Typescript compilation

After creating an Angular2 component, I wanted to share the code with my colleagues. Therefore, I uploaded the code to Github, cloned the repository, ran npm install, and then npm run tsc. However, I encountered the following errors: error TS2318: Cannot ...

Fixing the hydration error in Next 13 can be challenging, especially when it occurs outside of a Suspense boundary

Encountering an issue while working with Next.js 13: Error: Hydration failed because the initial UI does not match what was rendered on the server. Warning: Expected server HTML to contain a matching <div> in <html>. Every time I attempt to r ...

Creating a multi-step form in Rails without using the Wizard gem allows for more

My Rails 3.2.14 app gathers call data and the new/edit action form is quite lengthy on a single page. I am interested in implementing a multistep form using JS/client side processing to navigate between steps. While considering the Wicked Gem for multi-ste ...