Javascript's recursive function is only triggered once

I have written a code snippet to traverse through a JSON tree and extract specific keys. However, I am facing an issue where the recursion only works once for keys labeled as "complexType" which have a tree node with additional elements. Subsequent iterations do not get called.

function retrieveKeys(json){
    sequence = json["sequence"]["element"]        
    for(i=0;i<sequence.length;i++){
        innerSequence = sequence[i]         
        if(innerSequence["complexType"] != undefined){
            retrieveKeys(innerSequence["complexType"])
        }
    }
}

For the complete JSON file, you can refer to this link.

Answer №1

You have a global variable loop! In JavaScript, any references or assignments made without var, let, or const are considered global.

As a result, every time getStructure is called, i resets to 0 for all instances. The variables sequence and innerSequence are also treated as globals.

Furthermore, the function's name suggests it should return something other than undefined, which is its current outcome.

There are semicolons missing in your code. Utilizing "use strict" mode can help prevent situations where the browser interprets your code differently than intended.

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

Hey there, I'm looking to use different CSS fonts on Windows and Mac for the same page on a web application. Can someone please guide me on how to accomplish this?

In order to tailor the font based on the operating system, the following criteria should be followed: For Windows: "Segoe UI" For Mac: "SF Pro" Attempts have been made using the code provided below, but it seems to load before the DOM and lacks persisten ...

Transmitting the character symbol '&' within a string from Angular to PHP

My goal is to transmit a string, such as "the cat & the dog", from Angular to PHP using GET method. I have used encodeURI(note) in Angular and in PHP $note = $_GET['note']; $note = mysql_real_escape_string($note); However, when it gets inse ...

Angular's ng-submit directive does not use the most up-to-date scope available

I'm currently learning Angular and have been struggling with a particular issue. I have created a form to edit user details. Upon page load, I make an AJAX call to fetch the data of the selected user and populate the scope with the values. Initially, ...

Function for JavaScript Form Validation: Iterating through each element

I have a set of form input elements that need to be iterated through in order to validate them based on their name attributes. I utilized jQuery to accomplish this, using the .each method to loop through the elements and apply/remove a class name to those ...

Re-rendering multiple components with the new `use` feature in React version 18.3.0

When trying to fetch and use data using React 18.3.0, I encountered an issue with multiple re-rendering. react: 18.3.0-canary-3ff846d10-20230724 next: 13.4.12 The code for SuspenseTest component is causing multiple console outputs (about 5 to 8 times) be ...

Issue with Node Canvas/Resemble.js: The image provided has not finished loading during the load operation

Recently, I encountered a challenge while trying to utilize Resemble.js in a node environment. Despite some initial complications with installing canvas/cairo due to OS X Mavericks/XQuarts and Homebrew issues, I eventually succeeded. After making signific ...

Convert a JavaScript array of objects into an array of arrays

I'm searching for a more elegant ES6 solution to transform this array: var src = [{x:1,y:'a'},{x:2,y:'b'}]; Into this array: var desc = [[1,2],["a","b"]]; This new array should contain one array for all properties and another a ...

The onUpdate function does not seem to be functioning as expected in the react-custom-scrollbars

In my React.js application, I am utilizing the "react-custom-scrollbars" component. My goal is to invoke a method each time a user scrolls. To achieve this, I have attempted using the onUpdate method. <div className="thumbs-panel"> <Scrollbar ...

Utilizing VueJS and Lodash: The technique for extracting an array of objects exclusively featuring a specific key string

I am attempting to extract certain data from an Object that has a string _new attached to it. Explore the code on Codesandbox: https://codesandbox.io/s/vibrant-bardeen-77so1u?file=/src/components/Lodash.vue:0-353 This is what my data looks like: data.j ...

Implementing mouse hover functionality for fieldset in EXTJS

I am looking to enhance the following code snippet by adding a mouse hover event that changes the background color of the fieldset item and displays an image next to it. Can someone assist me with this modification? var mainGroup = { ...

AngularJS meta tags featuring dynamic asynchronous content

I am currently facing a challenge with my angular application that is running within a .net application. My main goal is to implement meta tags for SEO and other purposes. However, the issue I'm encountering is that I cannot determine the page title u ...

Is Eval really as bad as they say... What alternative should I consider using instead?

After making an ajax request, a JSON array filled with user inputs is returned to me. The inputs have already been sanitized, and by utilizing the eval() function, I can easily generate my JavaScript object and update the page... However, there lies a dil ...

I'm looking to send JSON data using jQuery's AJAX method - how can I

I was recently assigned a project with the following instructions: Develop an HTML page that will use our API endpoint to fetch a list of logs from our API logger and display them in a visually appealing grid. The page should automatically make this call e ...

Switching effortlessly between Fixed and Relative positioning

As I work on creating a unique scrolling experience, I aim to have elements stop at specific points and animate before returning to normal scroll behavior once the user reaches the final point of the page. Essentially, when div X reaches the middle of the ...

Triggered by each user interaction, the callback function is activated

I need to add a timeout feature to my AngularJS web application. Each time a user interacts with the site, a timer is set for 10 minutes. Once this timer runs out on the client-side, a request is sent to the server signaling a timeout. What is the best wa ...

Combine two JSON objects with the same key

I am facing an issue with two JSON objects that have a similar key question. var data = {"question":[{ QuestionID : counter1, QuestionText: question1, Choices:[{ChoiceID:100,Choice:"Yes",NextQuestionID:counter}, {ChoiceID:101,Choice: ...

Graph using chartjs-node-canvas in dark mode

I am currently incorporating the chartjs-node-canvas library into my Protractor test written in JavaScript. However, I am facing an issue where my chart appears completely black - to be more precise, only the white frame of the square is visible without an ...

How to initiate a download via a GET request in axios

I am currently utilizing Vue.js 2 with Axios to send a GET request and pass parameters to the server in order to retrieve a PDF as a response. The server is powered by Laravel. Having issues with force downloading the PDF even though the correct headers a ...

What is causing the warnings for a functional TypeScript multidimensional array?

I have an array of individuals stored in a nested associative array structure. Each individual is assigned to a specific location, and each location is associated with a particular timezone. Here is how I have defined my variables: interface AssociativeArr ...

Tips on modifying JSON information from various collections of key-value pairs

What is the best way to manipulate JSON data by changing values from multiple key-value pairs in Java? ...