Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6352393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:11:50+00:00 2026-05-24T22:11:50+00:00

The tutorial is here: Tutorial: Writing Spreadsheet data using JavaScript Objects The full code

  • 0

The tutorial is here: Tutorial: Writing Spreadsheet data using JavaScript Objects

The full code can be found at the end of the tutorial.

I don’t get the for loop in the first function, runExample()

  for (var i = 0; i < data.length; ++i) {
    var rowData = data[i];
    if (!dataByDepartment[rowData.department]) {
      dataByDepartment[rowData.department] = [];
      departments.push(rowData.department);
    }
    dataByDepartment[rowData.department].push(rowData);
  }

I don’t get what is going on inside the if-statement.
What do they mean by dataByDepartment[rowData.department]) ???

dataByDepartment is initially empty… Is this creating a property??

Can someone please explain what that whole loop is doing? Thank you very much!

PS: I am still quite new to Javascript… Coming from C programming I am always confused by the object and property creations…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:11:51+00:00Added an answer on May 24, 2026 at 10:11 pm

    In general terms the function of the loop is to populate the dataByDepartment object with one property for each (distinct) department, where each property will reference an array of data applicable to that department. Each iteration of the loop first checks whether there is already a property for the current department and if not it creates it. If it does need to create a new property it also adds the department to the departments array.

    Some background: the following statement creates an object with initially no properties:

    var dataByDepartment = {};
    

    To assign a property “key1” with the value “value1” to that object you would then say:

    dataByDepartment.key1 = "value1";
    // or
    dataByDepartment["key1"] = "value1";
    

    Note that if a property called “key1” already existed it would be overwritten. The square-bracket syntax allows you to use property key names that are variable. So you can say:

    var myKey = "key2";
    dataByDepartment[myKey] = "value2";
    

    Which will create a property with a name equal to whatever myKey evaluates to (“key2” in this case), and the value “value2”.

    So getting back to the actual code you quote, the if statement:

    if (!dataByDepartment[rowData.department]) {
    

    is checking whether dataByDepartment already has a property with a key name equal to whatever is in rowData.department. The syntax is a shortcut roughly equivalent to if (dataByDepartment[rowData.department] != undefined).

    The first statement in the if:

    dataByDepartment[rowData.department] = [];
    

    creates a new property with the key name of whatever is in rowData.department and the value of a new empty array. At that point if the property already existed it would be overwritten by a new empty array, hence the if test.

    The second statement in the if adds the department name to the departments array:

    departments.push(rowData.department);
    

    Finally, after the if, the array referenced by dataByDepartment[rowData.department] has a new element added to it:

    dataByDepartment[rowData.department].push(rowData);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a PyGtk paint program based on the basic tutorial found here .
I have found a Tutorial here on how to implement drag and drop in
I followed the tutorial found here and added my own flare to it: tutorial
I'm running through this tutorial found here: http://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htm which teaches how to pass a
I'm learning by reading this tutorial: Link Here's the code: <?php require_once 'Zend/Loader.php'; class
I am using Ubuntu 10.04. By following the tutorial here , I have created
I'm writing a XUL application using JavaScript for the coding. I would like to
I just started writing Plugins for jQuery. I found a good tutorial how to
Using: Ubuntu 11.04 Django 1.3 Python 2.7 Following the tutorial at Writing your first
I'm following the tutorial here http://applicake.com/posts/54-integrating-facebook-connect-with-rails-applications to set up facebook connect. It almost worked,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.