Results 1 to 4 of 4

Thread: associative array in JS

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    associative array in JS

    hi all,
    i have written an API in html5. and this is how i load it
    HTML Code:
    $(document).ready(function(){
    oChart= new _chart("canvas4");
    oChart.props.values= = [{ Name: "Rob", max:33, actual : 10, actual2: 15, color: "red" },
                              { Name: "Carole", max: 28, actual : 9, actual2: 17,color: "greenyellow" },
                              { Name: "David", max: 36, actual : 16, actual2: 8,color: "cyan" }						   					  
    						   ];;
    oChart.refresh();
    });
    the dilema is, figures in the values array should be updated every second, and i can do that by calling ajax method that returns a tring in the format
    [color][{ Name: "Rob", max:33, actual : 10, actual2: 15, color: "red" },
    { Name: "Carole", max: 28, actual : 9, actual2: 17,color: "greenyellow" },
    { Name: "David", max: 36, actual : 16, actual2: 8,color: "cyan" }
    ];[/color]
    how to evaluate the returned string into the values array ??
    thanks in advance ..

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: associative array in JS

    If you were to put quotes around the keys, it would become valid JSON, and then you could use JSON.parse to evaluate the string.
    Otherwise, you're limited to eval, which is not the best option because it parses and executes more than just data structures.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2009
    Location
    sydney
    Posts
    265

    Re: associative array in JS

    Thanks, eval() worked for me.. any ideas how to display data based on mouse position over the canvas
    here is an example: http://www.koolchart.com/demo/
    check out Barchart

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Re: associative array in JS

    In JS objects are also associative arrays or hashes.

    theStatus.Home

    can also be read.

    theStatus['Home']

    You can access a form by performing these DOM calls:

    document.forms['theForm']
    document.forms.theForm

Posting Permissions

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



Click Here to Expand Forum to Full Width