Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a very general question and the comments (and down-votes) are right, you should provide more information about what you've tried before posting. But I'm in a happy mood, so...</p> <p>That data is in a pretty bad format for looping through. If you have the ability to tweak it a little, do so. Anything that is one-to-many should be an array (one date has many scenarios). Anything that is one-to-one should be properties of the same object (e.g. scenario name and average). Once you data is in the 'right' format you'll need a nested loop for each level of the hierarchy. You have two levels (dates and scenarios) so you'll have two loops. Each will write a row.</p> <p>Then the general idea would be:</p> <pre><code>var dataSet = [{"Date":"2013/02/26", scenarios: [ {name: "Scn1","Avg": 500,"Count": 5,"Max": 700}, {name: "Scn2","Avg": 800,"Count": 6,"Max": 1000}, {name: "Scn3","Avg":500,"Count":5,"Max":700} ] }]; (function draw(data) { var i, j; for (i = 0; i &lt; data.length; i++) { $('#myTable').append('&lt;tr&gt;&lt;td colspan="4"&gt;' + data[i].Date + '&lt;/td&gt;&lt;/tr&gt;'); $('#myTable').append('&lt;tr&gt;'); $('#myTable').append('&lt;td&gt;Scenario&lt;/td&gt;'); $('#myTable').append('&lt;td&gt;Average&lt;/td&gt;'); $('#myTable').append('&lt;td&gt;Count&lt;/td&gt;'); $('#myTable').append('&lt;td&gt;Max&lt;/td&gt;'); $('#myTable').append('&lt;/tr&gt;'); for (var j = 0; j &lt; data[i].scenarios.length; j++) { $('#myTable').append('&lt;tr&gt;'); $('#myTable').append('&lt;td&gt;' + data[i].scenarios[j].name + '&lt;/td&gt;'); $('#myTable').append('&lt;td&gt;' + data[i].scenarios[j].Avg + '&lt;/td&gt;'); $('#myTable').append('&lt;td&gt;' + data[i].scenarios[j].Count + '&lt;/td&gt;'); $('#myTable').append('&lt;td&gt;' + data[i].scenarios[j].Max + '&lt;/td&gt;'); $('#myTable').append('&lt;/tr&gt;'); } } })(dataSet); </code></pre> <p>This is using jQuery to add elements to the DOM. There will be libraries and plugins to do this. If you want a quick fix, you may look at them. If you're beginning with JavaScript and want to be an expert one day, best to get your head around looping and traversing objects first.</p> <p>Note that to do all of this you have to make the test.json file available to your code. You would probably do this with AJAX, I'd suggest reading this: <a href="https://developer.mozilla.org/en/docs/AJAX" rel="nofollow">https://developer.mozilla.org/en/docs/AJAX</a></p> <p>Here's the working fiddle: <a href="http://jsfiddle.net/ub587/2/" rel="nofollow">http://jsfiddle.net/ub587/2/</a></p> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload