How can I transform a javascript object into a fresh array object?

Could you assist me with transforming JSON data?

The initial structure is as follows:

[
  {
    "_id": "606f990042cc89060c54a632",
    "number": 1293,
    "date": "2021-04-08",
    "__v": 0
  },
  {
    "_id": "606e478042cc89060c54a631",
    "number": 997,
    "date": "2021-04-07",
    "__v": 0
  }
]

Once transformed, the structure should look like this:

[
  {
    "id": "weight",
    "data": [
      {
        "x": "2021-04-08",
        "y": 1293
      },
      {
        "x": "2021-04-07",
        "y": 997
      }
    ]
  }
]

What would be the most efficient way to achieve this transformation? Would using the lodash library or JavaScript's built-in functions be better for this task?

Any hints on how I can approach this?

Answer №1

To achieve this, you can utilize the power of Array#map along with destructuring.

let data = [{"_id":"606f990042cc89060c54a632","number":1293,"date":"2021-04-08","__v":0},{"_id":"606e478042cc89060c54a631","number":997,"date":"2021-04-07","__v":0},{"_id":"606d3508c7f841e27c8a25a1","number":1068,"date":"2021-04-06"},{"_id":"606d3508c7f841e27c8a25a0","number":890,"date":"2021-04-05"},];
let result = data.map(({date:x,number:y})=>({x,y}));
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

What is the best method for dividing strings in javascript?

After invoking a JavaScript function, I received the following results: (1, 00), (2, 10), (3, 01), (4, 11) I am looking to store this data in an array or JSON format like this: [{id:1, number: 00},{id:2, number: 10},{id:3, number: 01},{id:4, number: 11} ...

Is there a way to show additional information beyond just the title in FullCalendar?

Hello, I am currently using the full calendar plugin and have a question. While I can display the title of events on my calendar, I would also like to display additional information from my database (such as full name or description) alongside the title. H ...

JavaScript: Changing the names of all object keys

As a beginner, I am struggling to rename some objects in my key using a map function. Below is my current array: "platforms": [ { "id": 6, "name": "PC (Microsoft Windows)" }, { "id": 11, "na ...

Select elements from a PHP loop

As part of my school project, I am developing a basic webshop. Currently, I am using a while loop to display featured products on the homepage. However, I now need to implement a shopping cart functionality. After a user clicks the "add to cart" button, th ...

Is it best to stick with a static page size, or

While I typically design my webpages dynamically by determining the screen size and creating divs accordingly, I'm curious about the advantages of using a 'static' sizing approach (such as using pixels). Any insights on this contrasting meth ...

Issue encountered while attempting to send a direct message to a user that has been mentioned

Attempting to create a reminder command in discord.js with two arguments: the message and the mentioned user to remind but encountering an error. Code snippet: client.on('message', (message) => { const { content, guild, channel, author } = m ...

Execute the JS file to retrieve the initial port available within the npm script

Within my package.json, I have the following script: "dev": "webpack-dev-server --config webpack.dev.js --progress --port ${getPort()}", I've also installed a package called get-port, which allows me to set a default port and ...

A React component that exclusively renders component tags

After successfully loading index.html with no JavaScript errors, I ran into an issue where nothing was rendering on the page. Upon inspecting the webpage, all I could see was a tag and nothing else. It turns out that I have a component called "list". The ...

What is the process for replacing a variable's value in a JSON object using Python?

#!/usr/bin/python import requests import uuid random_uuid = uuid.uuid4() print random_uuid url = "http://192.168.54.214:8080/credential-store/domain/_/createCredentials" payload = '''json={ "": "0", "credentials": { ...

Page elements subtly move when reloading in Chrome

I am experiencing an issue with a div that has left and top offsets randomly selected from an array of values upon page load. Most of the time, it works fine. However, occasionally, upon refreshing the page, the window scrolls down slightly, revealing the ...

There seems to be an issue with React-hook-form and material-ui not retaining value changes in the onBlur() method

Stepping into the realm of react-hook-form is a new experience for me. I'm putting in effort to grasp everything, but there are still some pieces missing from the puzzle. Seeking assistance akin to Obiwan Kenobi! The Dilemma at Hand: An <Textfiel ...

Solving the problem of 'json mime-type on 2003 server

I am currently facing an issue: On a 2003 server with iis 6, I have a functioning solution. However, during every deployment of the solution, I find myself having to manually configure the MIME type on the iis. Although I have implemented this in my web ...

Unable to display PHP response in a div using AJAX - issue persisting

Hey there! I'm currently working on a project where I want the form submission to load a response into a div without refreshing the page. I've looked at various examples of ajax forms with PHP online, but haven't been able to solve my issue ...

Having trouble getting my Leaflet map to display even after meticulously following the quick-start guide

I am experiencing an issue where the map is not displaying at all, leaving a blank space where it should be. Despite following Leaflet's quick-start guide, I have been unable to determine the cause of this problem. Here is the code that I currently h ...

SQL stores Array Input as a static identifier 'Array'

Previously, I was able to save an object in the database. However, now I need to save each individual data in an array within a row. I attempted to use array_column to store a specific set of data in a column, but it ended up saving as the word 'Array ...

Having trouble with submitting an Ajax form to a MySQL database

My expertise lies in PHP and HTML, but I'm currently learning JavaScript. I'm facing a challenge with creating a form that can submit data to be inserted into MySQL without reloading the page (using AJAX). Here is the form I have: <form id=" ...

Add a unique identifier to a table row in a jQuery/AJAX function without the need for a looping structure

My PHP query retrieves client data and generates a table with rows for each client. Each row contains a link with a unique ID attached to it. Clicking on this link triggers an AJAX function based on the client's ID, which opens a modal displaying info ...

Struggling to make npm and sqlite3 function properly on MacOS Catalina

Struggling with npm package installation in a project directory on my Mac has proven to be quite the challenge. Each attempt at a simple npm install results in perplexing errors that I can't seem to grasp. The hurdle seems to center around specific p ...

The button fails to trigger the AJAX request

I've developed a form that includes a QR code generator. The QR code is generated as an SVG, and below the code is a download button that should trigger an AJAX call to my PHP script when clicked. However, the download button does not seem to initiate ...

Looping through arrays with for loops

I have an array named blockHeights which consists of 3 values: 1, 2, and 3. This means that blockHeights[0] is equivalent to 1. Additionally, there is a loop present: for (int i = 1; i <= blockHeights.length; i++) At the start of the loop, I wish to ...