For Loop Using Dynamic Set


What Does This Code Snippet Do?

Unlike the incremental For Loop where you specify the number of iterations you want the loop to execute, this For Loop automatically does the same number of iterations as there are items within your data set. This makes it an ideal choice when you want to keep your code simple and not have to worry about figuring out the length of your data set. Which sometimes can't be done if you're working with an Object instead of an Array.



Why Is It Useful?

This method of using a For Loop allows your to iterate through both Arrays and Objects, while the incremental For Loop is really best for Arrays. The reason behind this is that you can't find the length of an object, so you'd have to figure out the correct number to pass through in your incremental For Loop. You could use Object.keys(object).length... but that wouldn't be helpful if you have a multiple objects with varying sizes.



How to Use This Code Snippet

Using this code snippet is simple. You probably don't even need it as a separate function since it's a basic loop statement, but let's take a look at it anyway.

function forLoopItems(data) { for(let item in data) { console.log(data[item]) } }

And while we're at it, let's take this object as an example:

let data = { id: 1, name: 'Bulbasaur', category: 'Seed', height: '2ft 4in', weight: '15.2lbs', types: ['Grass', 'Poison'], weaknesses: ['Fire', 'Ice', 'Flying', 'Psychic'] }

To iterate through this data, you would pass it through the code snippet's function like so:

forLoopItems(data) // Output: // 1 // Bulbasaur // Seed // 2ft 4in // 15.2lbs // [ 'Grass', 'Poison' ] // [ 'Fire', 'Ice', 'Flying', 'Psychic' ]

Now let's say you have an Array of different objects:

let data = [ { id: 1, name: 'Bulbasaur', category: 'Seed', height: '2ft 4in', weight: '15.2lbs', types: ['Grass', 'Poison'], weaknesses: ['Fire', 'Ice', 'Flying', 'Psychic'] }, { id: 2, name: 'Ivysaur', category: 'Seed', height: '3ft 3in', weight: '28.7lbs', types: ['Grass', 'Poison'], weaknesses: ['Fire', 'Ice', 'Flying', 'Psychic'] }, { id: 3, name: 'Venusaur', category: 'Seed', height: '6ft 7in', weight: '220.5lbs', types: ['Grass', 'Poison'], weaknesses: ['Fire', 'Ice', 'Flying', 'Psychic'] } ]

And you wanted to display each one's id, name, height, and weight. You could modify the code snippet like this:

function forLoopItems(data) { for(let item in data) { console.log('%s - %s | %s - %s', data[item].id, data[item].name, data[item].height, data[item].weight) } }

When you run the function again, you'll get this:

forLoopItems(data) // Output: // 1 - Bulbasaur | 2ft 4in - 15.2lbs // 2 - Ivysaur | 3ft 3in - 28.7lbs // 3 - Venusaur | 6ft 7in - 220.5lbs

That's a pretty simple way to modify the code snippet, but you can basically do whatever you want in it. For instance, maybe you want to convert an object array into a flat array so that you can import it into a Google Sheet?

thumbnail

NitroGAS Chrome Extension

Want to reference this code snippet right from your Google Apps Script Project? Check out our NitroGAS Chrome Extension. This tool will help you build your scripts faster than you could imagine. The tool itself and access to the code snippets are 100% FREE - Happy Coding!

Get the Extension