SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to access an object's members with array values

    I have an object that I want to access it's members by using a list of "headers" in an array that match the object's member names. I pull the values of the "headers" from a spreadsheet into an array (see log output at bottom). Then, when I iterate over the array using a for loop, I can't seem to access the Object's members. Why doesn't this work?

    HTML Code:
     var e = new Object();
      e.parameter = new Object();
      e.parameter.identifier = "test";
      //...
    
      Logger.log(Utilities.jsonStringify(e)); 
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var renters = ss.getSheetByName("Renters");
      
      var headers = ObjApp.camelArray(renters.getRange(1,1,1,renters.getLastColumn()).getValues()[0]);
      Logger.log(headers);
      var data = [];
      for(var h=0;h<headers.length;h++)
      {
        Logger.log(headers[h]);
        data.push(e.parameter[headers[h]]);
      };
    
      Logger.log(data);
    
    LOG OUTPUT:
    {"parameter":{"indentifier":"test"}}
    [identifier, address, tenants, gracePeriodDays, gracePeriodRecurrence, lateFeeAmount, startingBalance, startingDate, rentAmount, dayOfMonthDue]
    identifier
    address
    tenants
    gracePeriodDays
    gracePeriodRecurrence
    lateFeeAmount
    startingBalance
    startingDate
    rentAmount
    dayOfMonthDue
    [null, null, null, null, null, null, null, null, null, null]
    It seems to work in my second example when I don't pull the string value from an array over an iteration.
    Code:
    function test(e){
      var o = new Object();
      o.test1 = new Object();
      o.test1.test2 = 0;
      var test3 = "test2";
      Logger.log(o.test1[test3]);
      Logger.log(Utilities.jsonStringify(o));
    };
    
    LOG OUTPUT:
    0.0
    {"test1":{"test2":0}}

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    The first example seems to work when the spreadsheet parts are removed and the headers variable just contains an array of string values:

    Code javascript:
    var headers = ['identifier', 'address', 'tenants', 'gracePeriodDays', 'gracePeriodRecurrence', 'lateFeeAmount', 'startingBalance', 'startingDate', 'rentAmount', 'dayOfMonthDue'];

    I'd check what typeof says about what is stored at headers[h]
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •