www.webdeveloper.com
Results 1 to 6 of 6

Thread: javascript array

  1. #1
    Join Date
    Jan 2014
    Posts
    2

    javascript array

    Hi,
    I'm trying to passing a variable value to the array, it errors undefined. If I pass the same value explicitly then it works. Not sure what the problem is, can any one please help me on this?

    part -1 (this works)
    -------------------
    var dbcountval=3;
    for (var db2=0;db2<dbcountval;db2++)
    {
    var dbval=["13:52,2","14:52,4","15:52,20"];
    var dbfinalval=dbval[db2];
    document.getElementById("test_java").innerHTML=dbfinalval;
    }

    part -2 (this doesn't work)
    -------------------------

    var ReadVal=dbcpu(stime,etime);
    //above function returns value like "13:52,2","14:52,4","15:52,20"
    var dbval="[" + ReadVal + "]";
    //dbval value is ["13:52,2","14:52,4","15:52,20"]
    var dbcountval=3;

    for (var db2=0;db2<dbcountval;db2++)
    {
    var dbfinalval=dbval[db2];
    document.getElementById("test_java").innerHTML=dbfinalval;
    }

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,412

    Lightbulb

    Your first function would work better if you did this...
    Code:
      var dbval = ["13:52,2","14:52,4","15:52,20"];
      var dbfinalval = [];
    
      for (var db2=0; db2<dbval.length; db2++) { dbfinalval.push(dbval[db2]); }
      document.getElementById("test_java").innerHTML += dbfinalval;
    Why? Because the other elements of your dbfinalval array will be undefined.

    Your second part doesn't make sense to me.
    You cannot create an dbval array by concatenating a string between [ ] characters.
    Also, you have not defined the dbcpu() function.

    What is it that you are really confused about? It is unclear to me from your examples.
    Provide the full working and non-working code.

    Note also, you should enclose your scripts between [ code] and [ /code] tags (without the spaces)
    to make it easier to read, copy, test and debug by other forum members.

    Also (to be very picky), this is the javascript forum
    so the non-existent HTML element id="test_java" is not really correct nomenclature
    as you are not really testing any 'java' code.

  3. #3
    Join Date
    Jan 2014
    Posts
    2
    This is my whole function below. dbcpu function returns value like "13:52,2","14:52,4","15:52,20" and I concatenate the string between characters to use in array. My requirement is I need to get the values (13:52,2) one by one within loop.

    function Subgraph()
    {
    var ReadVal=dbcpu(stime,etime);
    var dbval="[" + ReadVal + "]";
    var dbcountval=3;
    for (var db2=0;db2<dbcountval;db2++)
    {
    var dbfinalval=dbval[db2];
    document.getElementById("test_java").innerHTML=dbfinalval;
    }
    }

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,412
    As requested before...
    Provide all pertinent function definitions. Show your function.

    Again, you CAN NOT create an array by concatenating strings.
    Check the error console in FF or Chrome browsers if you don't believe me.

    And, you should enclose your scripts between [ code] and [ /code] tags (without the spaces)
    to make it easier to read, copy, test and debug by other forum members.

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    If dbcpu returns value like "13:52,2","14:52,4","15:52,20", you have only to write
    Code:
    var dbval=[]; // shortcut for new Array();
    // For each new value
    dbval[dbval.length]=dbcpu(stime,etime)
    // To see the result 
    alert(dbval);

  6. #6
    Join Date
    Mar 2012
    Posts
    202
    Simple. Just use JSON.parse()

    Code:
    var ReadVal=dbcpu(stime,etime);
    //above function returns value like "13:52,2","14:52,4","15:52,20"
    var dbval=JSON.parse("[" + ReadVal + "]");
    //dbval value is ["13:52,2","14:52,4","15:52,20"]
    var dbcountval=3;
    
    for (var db2=0;db2<dbcountval;db2++)
    {
    var dbfinalval=dbval[db2];
    document.getElementById("test_java").innerHTML=dbfinalval;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles