Iterate through each key in a for loop and make a recursive function call to parse the JSON data

Assume there is a JSON object structured like this:

"bounds":{
    "coordinatetop":{
       "x":143,
       "y":544
    },
    "coordinatebottom":{
       "x":140,
       "y":510
    }
}

Currently, I am attempting to parse the JSON using the following code. The 'data' variable represents the JSON data and 'target' is an ID tag.

$.each(data, function(index, value) {
    if (typeof(value) == 'object') {
      processBounds(value, target);
    } else {
      console.log(value);
    }
});

While iterating through this code, the function call successfully retrieves values from 'coordinatetop' and 'target', extracting 'x' and 'y' values as expected. However, the function fails to iterate further to access the information stored in 'coordinatebottom'.

Are there alternative implementation methods that could help achieve this? Thank you!

Answer №1

Instead of recursively calling the function, you are simply linking a callback function to execute the processBounds() function.

You should define the processBounds function separately and then link it in the .each callback:

var processBounds = function(index, value) {
  if (typeof(value) == 'object') {
    processBounds(value, target);
  } else {
    console.log(value);
  }
}

$.each(data, processBounds);

Update:

$.each(data.bounds, processBounds);

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

Component missing dark mode feature

I recently started using the Dropdown component from NextUI and managed to set up the dark mode based on the Dark mode documentation. However, when I implemented the Dropdown, it appeared in the light theme instead of the dark mode that I had configured: ...

Be patient for the complete loading of the image during an AJAX operation

My webpage includes an ajax action that loads a div containing an image on the left and text on the right. The issue I am facing is that the text loads first, aligned to the left, and then the image loads, causing the text to shift to the right, resulting ...

Issues with npm @material-ui/core Button and TextField functionality causing errors and failures

I'm currently working on a React project and decided to incorporate the material-ui framework. After creating a component that includes a textfield and a button, I've encountered an issue where I can't interact with either of them as they s ...

What is preventing me from changing the text color of this span element to white?

For some reason, I'm trying to make the first two texts of each line appear in white, but my CSS doesn't seem to affect the span elements generated by JavaScript. Take a look at the code snippet below for more details. I'm still learning ho ...

Ways to navigate control or cursor to a different text box

On my website, I require customers to first enter their customer ID and click submit. Once validated, they are redirected back to the same page to enter a quote number. The quote number field is initially grayed out and cannot be edited. My issue is that ...

When using RS256 with JWT, the private key will not be accepted

I've been attempting to generate a JWT using 'jsonwebtoken' with RS256. The keys were generated using the following command: ssh-keygen -t rsa -b 4096 -m PEM -f <filename> The private key output appears as follows: -----BEGIN RSA PRIV ...

Utilizing quotation marks in ASP MVC4 when accessing Model values

I am currently working with a model in my view that includes a property named 'list of numbers' public myModel{ public string listOfNumber {get; set;} Within my controller, I assign a string value to this property public myController{ public ...

What is the best way to toggle a sticky footer menu visibility using div elements with JavaScript instead of relying on pixel measurements?

Just wanted to mention that I'm pretty new to this, so if I use the wrong terms, I apologize in advance. I am trying to replicate Morning Brew's sticky footer opt-in form (check it out here). However, the code I have now only tracks pixels. Is t ...

Creating a workaround for mapping legacy URL fragments to element names in Backbone.js

Is there a workaround for linking to specific parts of a page in a backbonejs application? I have found solutions for static HTML pages by using the `name` attribute and `#fragment` in the URL, but this approach doesn't seem to work directly in backbo ...

Using the power of jQuery, execute a function only once when the element is clicked

My goal is to use jQuery's .one method to change the color of an element only once, even if clicked again. However, I am having trouble getting it to work properly. Here is my HTML: <!DOCTYPE html> <head> <meta charset="UTF-8"& ...

What is the method for handling a get request in Spring3 MVC?

Within the client side, the following JavaScript code is used: <script src="api/api.js?v=1.x&key=abjas23456asg" type="text/javascript"></script> When the browser encounters this line, it will send a GET request to the server in order to r ...

Eliminate screen flickering during initial page load

I've been developing a static website using NuxtJS where users can choose between dark mode and default CSS media query selectors. Here is the code snippet for achieving this: <template> <div class="container"> <vertical-nav /> ...

What is the best way to retrieve CoinEx API using access ID and secret key in JavaScript?

Having trouble fetching account information using the CoinEx API and encountering an error. For more information on the API, please visit: API Invocation Description Acquire Market Statistics Inquire Account Info Note : This account is only for test p ...

Stop the interval when the variable equals "x"

I've created a function that controls a specific row in my database using AJAX. The function is triggered by a click event and placed within a setInterval function to check ten times per second. Initially, it will return 0, but eventually (usually wi ...

Guide to retrieving data from a JSON API

I'm struggling with a JSON file named runs.json, and I want to use Ajax to make a call to it. However, the specific value I need from the JSON is repeatedly showing as undefined. Here's an example of the JSON structure: { "status": "true", ...

Engaging with the jQuery form submission functionality

This question may seem simple, but as I am just starting to learn and understand jQuery, I apologize in advance. Imagine you have a form like the one below: <form id="form"> <input type="text" name="abc" /> <input type="text" name="def"/&g ...

Error installing npm: Dependencies could not be loaded for installation

Currently, I am in the process of learning Angular JS by building a simple phonecat app. Following the steps, I have downloaded Node.js and attempted to execute the command: npm install An error has occurred: C:>npm install npm ERR! install Couldn&ap ...

A WordPress website featuring the impressive capabilities of the Three.js JavaScript 3D library

I attempted to integrate the Three.js JavaScript 3D library into my WordPress website by including three.min.js in various parts: Within the body of a post <script src="/three.min.js"></script> In the footer <script type='text/ ...

Transferring JSON Data from DocumentDB (or CosmosDB) to Azure Data Lake

I currently have a vast amount of JSON files (in the millions) stored in Cosmos DB (formerly known as Document DB) and I am looking to transfer them to Azure Data Lake for cold storage. While searching, I came across this reference https://learn.microsoft ...

What could be causing the issue of React not showing the messages of "hello" or "goodbye"?

I have a page with a single button that is supposed to display either "hello world" or "goodbye world" when clicked. However, I am facing issues as the messages are not showing up as expected. Below is a screenshot of what the menu items look like when ca ...