What is the most efficient way to deep clone an object in JavaScript? Why does the US President use a new pen for each order? The code above will still work, but you can just remove the references to newObject in any call to the flattenObject function as well as in its function definition to remove the unnecessary reference. site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. Making statements based on opinion; back them up with references or personal experience. Does a chess position exists where one player has insufficient material, and at the same time has a forced mate in 2? The for/of loop has the following syntax: And if it's not an object, keep going and do something with that code, whether do a calculation, make an ajax call, or just log it to the console. Then when the number reaches 0, 1 is returned. In this case we are flattening out any nested objects into one single object and we will be assigning them to keys based on the object they were nested inside. How were scientific plots made in the 1960s? You've already defined 'i' as a variable in a broader scope ;). Stack Overflow for Teams is a private, secure spot for you and The better way to loop through objects is first to convert the object into an array. How does changing a guitar string's tuning affect its timbre? The JavaScript for/of statement loops through the values of an iterable objects. The tricky thing with nested objects is that some values--but not all--are objects themselves. Here, we can use typeof to check if the value at each key is an object. Nodes could anything, not just a DOM node, for example a plain object: javascript for loop variable and recursion, jsbin.com/yepoqixowe/edit?html,js,console, Episode 306: Gaming PCs to heat your home, oceans to cool your data centers. Can you post some example in jsfiddle.com ? Can an Order of Scribes Awakened Spellbook communicate in any way? That means we need to use recursion to iterate through those values, and do so until we the value is a string. – Stoive Jun 21 '11 at 0:41 Agree. Suppose that you need to develop a function that counts down from a specified number to 1. A company department is: Either an array of people. Use var to declare variables that are modified at recursion. i is declared locally and so it's not the same i variable getting modified. How to check whether a string contains a substring in JavaScript? Cache the length of the array so you would have the following: You should always cache especially when you're dealing with HTMLCollections. Let us understand this with pow function which is the shorthand form for power. If they were then we assign our value to our current key in our new object. If it is, we will call the function over again--recursion. The problem I have is that the subsequent call doesn't seem to produce any html, but it does show the key value pairs in the console. 2,979 4 4 gold badges 17 17 silver badges 38 38 bronze badges. Join Stack Overflow to learn, share knowledge, and build your career. (Don't return at this point, since that'll terminate the function) Note that typeof is a keyword, not a function - don't put parentheses after it. You can then loop through the values array by using any of the array looping methods. 1) A simple JavaScript recursive function example. A better way to recursively iterate through a JSON object In the above function, first of all we iterate over the main object and whenever we encounter a nesting we recursively iterate over the sub object search for the desired key, if we find the … Are creature environmental effects a bubble or column? rev 2021.1.21.38376, Stack Overflow works best with JavaScript enabled, Where developers & technologists share private knowledge with coworkers, Programming & related technical career opportunities, Recruit tech talent & build your employer brand, Reach developers & technologists worldwide. We check to see if our value is a nested object. Objects are not iterable and don’t have the forEach() method. Object.entries That means we need to use recursion to iterate through … for/of lets you loop over data structures that are iterable such as Arrays, Strings, Maps, NodeLists, and more. This will allow the rest of the object's keys that do not match the "newKey" to stay the same after the recursive calls. As was pointed out in comments, you do not actually need to pass newObject to the flatten helper function as an argument because the value of newObject is closed over at the time of the function call.