For Loop Using Increments


What Does This Code Snippet Do?

This code snippet is a basic For Loop that takes in a set iteration length and also lets you specify where in the data set to start the loop from. You might have seen this written as:

for(let i = 0; i < data.length; i++) { // Do Stuff }

This basic For Loop lets you iterate through an Array of data and stops the loop once it has finished going through every position within the Array.

This code snippet lets you specify where to start (as opposed to the first position) and also lets you specify how many iterations you want the loop to run for (as opposed to going through the whole thing).

Why Is It Useful?

This code snippet is useful when you're working with a data table that contains a header that you want to ignore and only want to look at a subset of the data (perhaps there's a footer that you'd like to ignore too!)

Here's an example of an data set that you might have pulled from a Google Sheet which represents an invoice.

[ ['id', 'name', 'cost'], ['123', 'Smart RGB Light Bulbs', '$175'], ['321', 'Wireless AR Remote', '$50'], ['456', 'RGB Controller Switch', '$150'], ['654', 'Ceiling Pendant Light Mount', '$125'], ['', 'Invoice Total', '$500'], ]

In this data set, you don't really care about the header which contains the column names nor do you care about the last row which contains the invoice total. What you do care about is getting all of the items in the invoice to calculate tax. To do this, you would want to start your For Loop from the second positoin and stop right before the last row - Like this:

const start = 2 const end = data.length - 1 let taxableAmount = 0 for(let i = 2; i < data.length - 1; i++) { let itemCost = data[i][2] taxableAmount += itemCost } const tax = taxableAmount * salesTax // Whatever the sales tax percentage is

How to Use This Code Snippet

To use this code snippet, first copy in the function into your Google Apps Script project - you can put it anywhere you like.

function forLoopIncremental(data, start, end) { for(let i = start; i < end; i++) { console.log(data[i]) } }

When you want to call it, just pass through your data, the position you want to start from, and lastly how many iterations you want the loop to run for.

const data = [1,2,3,4,5] const startPosition = 2 const iterations = 4 forLoopIncremental(data, startPosition, iterations) // Output: 2,3,4,5

The code snippet was written to just console.log() the data, but you can do anything you want with it. To customize this code snippet for yourself, just replace the console.log(data[i]) part with your own code/logic. Just make sure to use data[i] to reference the data being used for that specific iteration of the loop.


Be sure to check out our code snippet for using For Loops with a dynamic data set for another method for iterating through your data.

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