Guide on looping through a collection of objects that have child objects and generating a new custom object

I am currently working with an object list retrieved from an API. The response looks something like this:

  {
        "1/22/20": {
            "new_daily_deaths": 0,
            "total_cases": 1,
        },
        "1/23/20": {
            "new_deaths": 0,
            "total_cases": 10,
        },
        "1/24/20": {
            "new_deaths": 0,
            "total_cases": 20
        }
  }

The expected output should look like this:

{
   x:1/22/20,1/23/20,1/24/20 // key of every object
   y:1,10,20 //total_cases
 }

I need help figuring out how to achieve this desired format. I have already attempted using object.stringify, but it's not providing the expected result.

Answer №1

To achieve this, you can utilize the Object.keys() and Object.entries() methods as demonstrated below:

const data = {
        "1/22/20": {
            "new_daily_deaths": 0,
            "total_cases": 1,
        },
        "1/23/20": {
            "new_deaths": 0,
            "total_cases": 10,
        },
        "1/24/20": {
            "new_deaths": 0,
            "total_cases": 20
        }
  }

const keys = Object.keys(data).join(',')
const cases = Object.entries(data).map(([k,v]) => v).map(x=>x.total_cases).join(',')
const result = { x: keys, y: cases}
console.log(result)


The expected output is {x:1/22/20, y:1}, {x:1/23/20,y:10}

const data = {
        "1/22/20": {
            "new_daily_deaths": 0,
            "total_cases": 1,
        },
        "1/23/20": {
            "new_deaths": 0,
            "total_cases": 10,
        },
        "1/24/20": {
            "new_deaths": 0,
            "total_cases": 20
        }
  }

const result = Object.entries(data).map(([k,v]) => ({x: k, y: v.total_cases}))
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

Connect-busboy causing NodeJS issue: TypeError - the method 'on' cannot be called on an undefined object

Recently I encountered an issue with a piece of my code: router.route("/post") .get(function(req, res) { // ... }) .post(authReq, function(req, res) { // ... // Get uploaded file var fstream; req.pipe(re ...

Dealing with complications in the Rails asset pipeline management

I am working on a webpage that requires real-time data to be displayed. Therefore, it necessitates continuous ajax communication post page load. similar to this, jQuery -> setInterval -> $.ajax url: "some url" success: (data, te ...

The information contained in HTML element lists and paginated using bootstrap and React does not persist after changing pages. This issue affects the functionality of React + Bootstrap

My "Pagination" Component manages the pagination of items (typically test questions) passed in as parameters. The component displays these items on the screen based on the current page. const dataTest = [ <><div className="row&quo ...

What is the reason for the input type number in the <input> field being passed as a string?

I am currently developing a straightforward component that allows the user to input the balance they wish to add. Here is the template used for taking input from the user: <h3>Add Balance:</h3> <input #payment type="number"> < ...

Retrieve data from Ansible task result and assign it to a new variable

I am currently utilizing the elb_application_lb_info module to access information about my application load balancer. Below is the code snippet that I have implemented: - name: Test playbook hosts: tag_elastic_role_logstash vars: aws_access_key: AK ...

Generate an array of HTML files from external sources and randomize their placement within multiple div containers

I am currently working on a website that features a grid made up of divs (4x4) which need to have a set of texts shuffled into them every time the page is reloaded. You can see what it looks like here. Within my index.htm file, I have the following struct ...

Issue with Vuetify server side Datatable filtering in Vue.js未解决

I'm currently utilizing vuetify to handle my datatable functionality, including pagination and sorting. However, I've encountered an issue with the search filter. Although the response data from the search filter is correct, it's not getting ...

Transforming a Collection of Dictionary Objects into a JSON Array on iOS

I'm looking for a way to convert an NSArray into a JSON array format so that I can send it to the server. Here is a sample of the NSArray that needs to be converted: array([0] => array('latitude'=>'10.010490', ...

Implementing a collapsible menu feature in Bootstrap 3 when clicked

Experiencing an issue with a Bootstrap menu where I want the menu to collapse after clicking on one of the sections. This is the HTML code: <div class="container" id="home"> <nav class="navbar navbar-default navbar-fixed-top" role="navig ...

Remove any actions that a view has linked to the app.vent

By invoking view.unbindAll(), will the events that I registered on the global event bus be removed? ...

Nightwatch.js custom assertion for array comparison fails to function as expected

Utilizing the nightwatch-cucumber framework, which is built on top of Nightwatch.js, I am in the process of creating automated end-to-end tests. As a newcomer to 'JavaScript' and 'node.js', I encountered an error during the execution of ...

Is it possible to utilize Webpack 5's ChunkGroup API with several entries?

I am encountering an error message when attempting to upgrade from Webpack 4 to Webpack 5. The error states: Module.entryModule: Multiple entry modules are not supported by the deprecated API (Use the new ChunkGroup API) I have searched for information o ...

Leverage the Power of Two AngularJS Factories

Can I efficiently use two Factories in AngularJS by calling one from the other? Here's the Scenario: I have a Factory that returns an Array. This Factory is responsible for checking if the data to populate this Array already exists in local SQL Stor ...

Ensuring the input field and button are positioned in a horizontal line

Currently, I am developing a to-do list application using React and Chakra UI. I am trying to align the input field and the button on the same line for a more organized layout. This is the current layout: image I aim to achieve a design similar to this: ...

Is your string being truncated when sending it from iOS to a web server using POST?

Recently, I encountered an issue while attempting to send a serialized object from my iOS app to a web server. Everything seemed to be working fine when sending a large string, but as soon as I tried sending the serialized object, the data would get cut of ...

Android is now asking for location permissions instead of Bluetooth permissions, which may vary depending on the version

I am currently troubleshooting a React Native application that relies heavily on Bluetooth permissions. However, I am encountering an issue with the Android platform where the Bluetooth permissions are appearing as unavailable. I have configured the permi ...

Splitting the div into two columns

I've encountered various solutions to this issue, but when I integrate an Angular2 component inside the divs, it fails to function properly. Here is my progress so far: https://i.stack.imgur.com/qJ8a9.jpg Code: <div id="container"> <div ...

What is the best way to retrieve the specific property from a bound function?

I'm looking to retrieve the value of a specific property from a function that has already been bound. function foo(){ console.log(this.a) } const bar = foo.bind({a:1}) bar() // Outputs 1 bar.getThis() // expected result is { a: 1 } Given the code ...

Differences in Angular.js/jQuery html string parsing between versions 1.9.1 and 1.8.3

When attempting to use angular.element(stringWithHtmlStructure);, an error is thrown: Error: Syntax error, unrecognized expression: <div id="foo">bar</div> This issue occurs in jQuery 1.9.1 but not in 1.8.3. Is this a bug or a deliberate sec ...

Showing the values of two distinct select boxes in a URL

Here are both select boxes, the goal is to display selected values from both in the URL. This can be achieved by displaying them like this: e.g www.example.com#135+#140 OR www.example.com#135&140 (The order of values doesn't matter as long as bot ...