Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing a string to a function that formats JSON jquery
    text
    copied!<p>I have a feeling this is very simple, however, I am having trouble getting this to spit out anything other than <strong>undefined</strong> </p> <p>I would like to be able to grab the JSON object passing in a string to the function. Is this possible with a function like below?</p> <pre><code>var dashboardData= { "Dashboard": { "Total_Downloads":95, "Unique_Downloads":30, "Campaign_Threshold":1000, "Downloads_By_Date":[ {"Downloads":6,"Date":"9/6/2012 12:00:00 AM"}, {"Downloads":2,"Date":"9/7/2012 12:00:00 AM"}, {"Downloads":9,"Date":"9/20/2012 12:00:00 AM"}, {"Downloads":2,"Date":"9/22/2012 12:00:00 AM"}, {"Downloads":6,"Date":"9/23/2012 12:00:00 AM"}, {"Downloads":10,"Date":"9/24/2012 12:00:00 AM"}, {"Downloads":1,"Date":"9/26/2012 12:00:00 AM"}, {"Downloads":1,"Date":"9/29/2012 12:00:00 AM"}, {"Downloads":3,"Date":"10/2/2012 12:00:00 AM"}, {"Downloads":4,"Date":"10/5/2012 12:00:00 AM"}, {"Downloads":1,"Date":"10/6/2012 12:00:00 AM"}, {"Downloads":2,"Date":"10/8/2012 12:00:00 AM"}, {"Downloads":4,"Date":"10/9/2012 12:00:00 AM"}, {"Downloads":3,"Date":"10/10/2012 12:00:00 AM"}, {"Downloads":1,"Date":"10/12/2012 12:00:00 AM"}, {"Downloads":27,"Date":"10/16/2012 12:00:00 AM"}, {"Downloads":2,"Date":"10/17/2012 12:00:00 AM"}, {"Downloads":1,"Date":"10/19/2012 12:00:00 AM"}, {"Downloads":1,"Date":"10/20/2012 12:00:00 AM"}, {"Downloads":9,"Date":"10/21/2012 12:00:00 AM"} ], } }; function formatJSON(source, key1, key2) { var i, l, dest = [], row; for(i = 0, l = source.length; i &lt; l; i++) { row = source[i]; dest.push('[' + row.key1, row.key2 + ']'); } return '[' + dest + ']'; } var downloads= formatJSON(dashboardData.Dashboard.Downloads_By_Date,'Downloads','Date'); var output="&lt;ul&gt;"; output+="&lt;li&gt;" + dashboardData.Dashboard.Total_Downloads + "&lt;/li&gt;"; output+="&lt;/ul&gt;"; document.getElementById("placeholder").innerHTML=downloads; </code></pre> <p>​</p>
 

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