Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've written a function that loops though all of the accounts, properties, views, and filters - each 'level' requires another for loop. I'm working with the Google Analytics Management API and Google Spreadsheet Script Editor.

Wondering how I could split up all of these loops into their own functions, or any other tips.

I also realize that I could cache the lengths in the loops to add some speed. I was looking around at changing it from for loop to forEach, but didn't see much of a speed improvement.

function _getViewFilterData(inAccountsList) {
  var accountsList = Array.isArray(inAccountsList) ? inAccountsList : accounts.list().getItems();
  var account = {};

  var propertiesList = '';
  var property = {};

  var profilesList = '';
  var profile = {};

  var filtersList = '';
  var filter = {};

  var i, j, k, l;

  var results = {};

  results.data = [];
  results.sheetName = 'viewFilters';
  results.header = [
    'account.name',
    'account.id',
    'property.name',
    'property.id',
    'profile.name',
    'profile.id',
    'filter.name',
    'filter.id'
    ];


  for (i = 0; i < accountsList.length; i++) {
    account.name = accountsList[i].name;
    account.id = accountsList[i].id;

    propertiesList = properties.list(account.id).getItems();

    for (j = 0; j < propertiesList.length; j++) {
      property.name = propertiesList[j].name;
      property.id = propertiesList[j].id;

      profilesList = profiles.list(account.id, property.id).getItems();

      for (k = 0; k < profilesList.length; k++) {
        profile.name = profilesList[k].name;
        profile.id = profilesList[k].id;

        filtersList = viewFilters.list(account.id, property.id, profile.id).getItems();

        for (l = 0; l < filtersList.length; l++) {

          filter.name = filtersList[l].filterRef.name;
          filter.id = filtersList[l].filterRef.id;

          results.data.push([
            account.name,
            account.id,
            property.name,
            property.id,
            profile.name,
            profile.id,
            filter.name,
            filter.id
          ]);   
        }
      }
    }
  }

  return results;
}
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.