Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 3 of 3 FirstFirst 123
Results 31 to 38 of 38
  1. #31
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by martynball View Post
    Alright, seems like it all works, but what the heck is going on here:

    Lets say I create it manually like so

    sales["N712"] = [];
    sales["N712"]["LR"] = 0;


    Now below that code I make an alert

    alert(sales["N712"]["LR"]);


    But then I get this error
    Code:
    Unable to get property "N712" of undefined or null reference
    Referencing the alert.
    it means sales is undefined
    set sales equal to []

  2. Users who have thanked DaveyErwin for this post:

    martynball (08-01-2013)

  3. #32
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Can you post all the related code? It seems like the sales var is undefined.

  4. #33
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Well, I just moved the alert out of a function it was in and now it works...

    Code:
    // JavaScript Document
    var sales = [ ];
    
    var products = { 
    	"KR" : 4,
    	"LR" : 6 
    };
    
    sales["N712"] = [];
    sales["N712"]["LR"] = 0;
    
    //Send the sales to ASP to add to database
    function submitSales(sales) 
    {
    	alert(sales["N712"]["LR"]);
    
    ...
    Why does it show an error.. someone said on another topic
    Avoid relying on global variables. Pass in the sales variable as an argument.

    Which is what I did but it causes the error, if I remove "sales" from the function args it works.

  5. #34
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You don't show where you *CALL* the submitSales function.

    Almost surely that is the source of the latest problem.

    And it's not a great idea to use the same name for both the local and global variable.

    I would do something like:
    Code:
    var sales = [ ];
    
    var products = { 
    	"KR" : 4,
    	"LR" : 6 
    };
    
    sales["N712"] = [];
    sales["N712"]["LR"] = 0;
    
    //Send the sales to ASP to add to database
    function submitSales(salesObj) 
    {
    	alert(salesObj["N712"]["LR"]);
    }
    
    // invoke the function:
    submitSales(sales);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  6. #35
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Crap! I forgot about the actual CALL!

  7. #36
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
    var sales = [ ];
    
    var products = { 
    	"KR" : 4,
    	"LR" : 6 
    };
    
    sales["N712"] = [];
    sales["N712"]["LR"] = 0;
    
    //Send the sales to ASP to add to database
    function submitSales(salesObj) 
    {
    	alert(salesObj["N712"]["LR"]);
    }
    
    // invoke the function:
    submitSales(sales);
    The more usual way to write that JavaScript (to avoid the confusion between objects and arrays) would be:

    Code:
    var sales = {};
    
    var products = { 
    	"KR" : 4,
    	"LR" : 6 
    };
    
    sales.N712 = {};
    sales.N712.LR = 0;
    
    //Send the sales to ASP to add to database
    function submitSales(salesObj) 
    {
    	alert(salesObj.N712.LR);
    }
    
    // invoke the function:
    submitSales(sales);
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  8. #37
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Yes, but read this entire thread.

    He will actually be getting the "712", "KR", "LR" from outside code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  9. #38
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Old Pedant View Post
    Yes, but read this entire thread.

    He will actually be getting the "712", "KR", "LR" from outside code.
    It all works now, and I did what you said, changed the ID to be "N712" in the object.


 
Page 3 of 3 FirstFirst 123

Posting Permissions

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