Retrieve both the keys and values from a deeply nested JSON object

My goal is to retrieve JSON data with a specific structure as shown below:

{"Points": 
       {"90": 
             {"0": {"name": "John Phillip", "slug": "john"},
             {"1": {"name": "Mark Anthony", "slug": "mark"},
             ...
       },
       ...
 }

In my asynchronous function getData(93), I am fetching the data and processing it accordingly:

 async getData(93) {
      const res = await fetch("/json/sample.json");
      const data = await res.json();

      // round to the lowest multiple of five 5: 92->90, 93->90
      const roundToLowest5 = x => Math.floor(x/5)*5

      // Accessing the key '90' from the outer key 'Points'
      console.log(data.Points[roundToLowest5(value).toString()])

      return this.setState({ data });
    }

The console currently displays all data for the outer key 'Points':

{0: {…}, 1: {…}, 2: {…}, 3: {…}, 4: {…}, 5: {…}, 6: {…}, 7: {…}, 8: {…}, 9: {…}, 10: {…}, 11: {…}}

Since all index keys ("0", "1", "2"...) are strings, how can I retrieve all key-value pairs for each index key?

Desired output:

{"name":"John Phillip", "slug": "john"}, {"name":"Mark Anthony", "slug":"mark"}...

Answer №1

To get an array of values from all keys, you can use the Object.values method

console.log(Object.values(data.Points[roundToLowest5(value).toString()]))
// This code will output [{"name":"John Phillip", "slug": "john"}, {"name":"Mark Anthony", "slug":"mark"}...]

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

Transforming a Delta table into JSON within Azure Data Factory or Azure Logic Apps

I am facing a challenge with large tables, each containing over 50 million records stored in delta format within Azure Data Lake Storage Gen2. My goal is to convert these tables into JSON format using either Azure Data Factory or Azure Logic Apps. This wil ...

A variable in Javascript storing data about jQuery DOM elements

Currently, I am using the slick slider for my development (http://kenwheeler.github.io/slick/) In JavaScript, there is an event that returns a variable called 'slick' When I print the variable using console.log, this is what I see: https://i.s ...

Transforming a large JSON dataset into a CSV format

Seeking help with converting a massive JSON file containing tweet data (48MB) to CSV format for importing into a SQL database and cleansing purposes. Despite attempting multiple JSON to CSV converters, they all error due to the file size. Is there an effi ...

Getting data from a wordpress database using javascript

After setting up a table named inspirationWall in my database with id and Visit_Count as INT, I inserted a row with id=1 and Visit_Count = 2. Now, attempting to fetch the Visit_Count value in WordPress and show it in an alert for testing purposes. I have ...

Stop images from appearing transparent when placed inside a transparent div

My issue is clearly visible in this image: The div element is transparent and affecting the images inside it. Below is my HTML code: <div id="cselect" style="position: absolute; top: 99px; left: 37px; display: block;"> <div class="cnvptr"> ...

javascript issue with onchange query

The JavaScript snippet below is included in the head section of my file. <?php echo "<script language='JavaScript'>\n"; echo "var times = new Array();\n"; echo "times[0] = 0;\n"; foreach($times as $time) { echo "times[". ...

Is it possible for AngularJS components to alter their enclosing element?

I see Angular components as element directives. Take a component named hero, for example, I can include it in the parent template like so: <hero myparam="something"></hero> What I envision is using the hero element as a container managed by t ...

Having trouble retrieving an object through a GET request in Node.js with Express

Currently, I am going through a tutorial on Node.js by Traversy Media and I have encountered an issue that has me stumped. The problem arises when I attempt to retrieve the response of a single object from an array of objects using the following code: app ...

Can someone clarify the reason behind creating this graph using vega?

I'm attempting to create a graph in vegalite that displays the progression of stock prices over time. It seems like this should be a simple task, but I'm running into an issue where only two lines are being displayed and they don't accuratel ...

Data from AngularFire not displaying in my list application

While going through tutorials on the Angular website, I encountered a roadblock while attempting to create a list that utilizes Firebase for data storage. Strangely, everything seems to be functional on the Angular site, but clicking on the "Edit Me" link ...

What methods can I use in Mocha with Sinon for testing multiple callbacks?

My code involves a function with asynchronous operations and multiple callbacks var f = (cb1, cb2) => { return new Promise((resolve, reject) => { /* ... */ }); }; During testing, I wanted to use sinon to create spies var cb1Spy = sinon.spy(); va ...

Track and manage date ranges inclusive of specific times

http://jsfiddle.net/7vzapm49/1/ var startdatearr = [new Date("04 Dec 2014 14:30:00").toUTCString(), new Date("07 Dec 2014 14:30:00").toUTCString()]; var enddatearr = [new Date("05 Dec 2014 14:30:00").toUTCString(), new Date("08 Dec 2014 14:30:00").toUTCSt ...

Ways to Determine the Height and Width of an Image Once it has been Adjusted for a View

Is there a way to retrieve the height and width of an image after it has been resized for a view? I have images that may vary in original dimensions, but users can resize them as needed. For example, this code from the console gives the client height: do ...

The autocomplete feature in React is not displaying properly due to a problem with rendering

I am currently working on creating an autocomplete/autosuggest search bar using the Material-UI library with data fetched from an API. Below is a step-by-step breakdown of the code implementation. We first define the options for the autosuggest search bar ...

Wavy CSS Animation

For assistance with my spinning image, please check out this fiddle: https://jsfiddle.net/dricardo1/9a8p6srb/ Hello! I'm struggling with a spinning image that has a noticeable wobble when rotating. I can't seem to find a solution to make the rot ...

JavaScript's Ajax request seems to be stagnant and inactive

Having some difficulties with the code below. It triggers an alert correctly, but the ajax part doesn't seem to be functioning. No errors or indications of what's wrong. $(document).on('change', '.department_select', function ...

When making a request through local apache, the POST method is switched to GET

I've been attempting to send a post request using the code below. However, the request is being sent as a GET instead of POST. How can I resolve this issue? $.ajax({ url: 'https://www.exampleurl.com', method: 'POST', h ...

Is there a way to create optional sections within a reusable component's template in a Vue 3 application?

Recently, I've been immersed in developing a Single Page Application (SPA) using the powerful combination of Vue 3, TypeScript, and integrating The Movie Database (TMDB) API. One interesting challenge I encountered was with my versatile movie card co ...

In Laravel, saving null data of JSON type is stored as [null]

I am attempting to save JSON data in my database. Here is the migration: $table->json('email_signature')->nullable(); In the model, I have specified: protected $casts = [ 'email_signature' => 'array' ]; Within ...

Error messages arising from JSON files generated by PHP

My current setup involves using a PHP script to generate a cache of JSON files. We utilize a PHP file on our server to interact with a large database, fetching result sets in JSON format for use with jQuery and other frameworks on our site. The core scrip ...