| | |
Trying to parse through javascript collection object using Prototype
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
I have the following nested javascript collection (below) and I'm trying to access the information within the collection using prototype so I can properly style and layout the data. I'm stuck and would like to know if anybody could assist.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var teams = { "team1:" { "QB": "Alexander Hamilton", "RB": "John Jay", "WR": "James Madison" }, "team2: { "QB": "George Washington", "RB": "John Adams", "WR": "John Hancock" } };
You could try this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8" standalone="no"?> <?xml-stylesheet type="text/css" href="#css21" media="screen"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html id="xhtml10S" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head profile="http://www.w3.org/2005/10/profile"> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <meta http-equiv="Window-target" content="_top" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>Free Live Help!</title> <style id="css21" type="text/css" media="screen"> /* <![CDATA[ */ /* ]]> */ </style> <script id="javascript1.5" type="text/javascript"> // <![CDATA[ var Teams = function() { }; Teams.prototype = { team1 : { QB : "Alexander Hamilton", RB : "John Jay", WR : "James Madison" }, team2 : { QB : "George Washington", RB : "John Adams", WR : "John Hancock" } }; var teams = new Teams(); window.onload = function() { var div = (( document.getElementById ) ? document.getElementById("main") : document.all.main ); for ( var team in teams ) { div.innerHTML += "<h3>" + team + "</h3>\n<p>"; for ( var member in teams[ team ] ) { div.innerHTML += "Type : <span style=\"color : green\">" + member + "</span> - <span style=\"color : blue\">" + teams[ team ][ member ] + "</span><br />"; } div.innerHTML += "</p>"; } } // ]]> </script> </head> <body> <div id="main"></div> </body> </html>
•
•
Join Date: Jun 2009
Posts: 49
Reputation:
Solved Threads: 3
Not realy sure, about what are you in doubt. Read more about the object definition and try this example:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <script> var teams = { "team1": { "QB": "Alexander Hamilton", "RB": "John Jay", "WR": "James Madison" }, team2: { // key need not to be quoted "QB": "George Washington", RB : "John Adams", "WR": "John Hancock" } }; </script> </head> <body> The use of object teams: <br> <script> document.write(teams.team1.RB);document.write("<br>"); document.write(teams["team2"].WR);document.write("<br>"); document.write(teams['team1']["QB"]);document.write("<br>"); document.write("<br>"); for (var index in teams.team1) { document.write(index+" "+teams.team1[index]);document.write("<br>"); } document.write("<br>"); for (var teamix in teams) { for (var index in teams[teamix]) { document.write(teamix+" / "+index+" is: "+teams[teamix][index]);document.write("<br>"); } document.write("<br>"); } </script> </body> </html>
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
Thanks,
•
•
•
•
I get the following json object back from a json_encode command that I'm using on the server-side. Is it possible to parse through this using javascript since it is missing names of inner objects (team1 and team2). If so, how?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
[ {"PlayerName":"Ron Artest","Position":"Forward","Height":"6-7","Weight":"260","College":"St. Johns"}, {"PlayerName":"Kobe Bryant","Position":"Guard","Height":"6-6","Weight":"205","College":"Lower Marion High Sc"} ]
•
•
•
•
You could try this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8" standalone="no"?> <?xml-stylesheet type="text/css" href="#css21" media="screen"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html id="xhtml10S" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head profile="http://www.w3.org/2005/10/profile"> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <meta http-equiv="Window-target" content="_top" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>Free Live Help!</title> <style id="css21" type="text/css" media="screen"> /* <![CDATA[ */ /* ]]> */ </style> <script id="javascript1.5" type="text/javascript"> // <![CDATA[ var Teams = function() { }; Teams.prototype = { team1 : { QB : "Alexander Hamilton", RB : "John Jay", WR : "James Madison" }, team2 : { QB : "George Washington", RB : "John Adams", WR : "John Hancock" } }; var teams = new Teams(); window.onload = function() { var div = (( document.getElementById ) ? document.getElementById("main") : document.all.main ); for ( var team in teams ) { div.innerHTML += "<h3>" + team + "</h3>\n<p>"; for ( var member in teams[ team ] ) { div.innerHTML += "Type : <span style=\"color : green\">" + member + "</span> - <span style=\"color : blue\">" + teams[ team ][ member ] + "</span><br />"; } div.innerHTML += "</p>"; } } // ]]> </script> </head> <body> <div id="main"></div> </body> </html>
Hi Gary,
here's your code, i've extrcted all of its collections with the used of a table, but if don't like my concept of using table to differentiate the items inside the players collections -- then simply re-apply the same procedure in my original post, using the
All codes is as follows:
hope it helps...
here's your code, i've extrcted all of its collections with the used of a table, but if don't like my concept of using table to differentiate the items inside the players collections -- then simply re-apply the same procedure in my original post, using the
innerHTML method.All codes is as follows:
javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html id="xhtml10" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- W3Cs Standard Template : XHTML 1.0 Transitional DTD --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>www.daniweb.com</title> <style type="text/css"> /* <![CDATA[ */ table { border-collapse : collapse; border : 1px solid #C0C0C0; color : #365895; text-align : center; width : 100%; } caption { width : auto; text-align : left; margin-bottom : .500em; padding-left : .500em; } td { border-top : 1px solid #C0C0C0; border-right : 1px dashed #C0C0C0; padding : .350em; width : auto; vertical-align : middle; letter-spacing : 2px; } thead td { background-color : #F0FFFF; border-right : 1px dashed #C0C0C0; padding : .350em; font : bold 80%/1 "Trebuchet MS", Verdana, Arial, sans-serif; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ window.onload = function() { if ( document.createElement ) { var players = [ { PlayerName : "Ron Artest", Position : "Forward", Height : "6-7", Weight : 260, College : "St. Johns" }, { PlayerName : "Kobe Bryant", Position : "Guard", Height : "6-6", Weight : 205, College : "Lower Marion High Sc" } ]; var container = document.getElementById("view"); var labels = [ "Name", "Position", "Height", "Weight", "College" ]; var table = document.createElement("table"); var tCaption = table.createCaption(); tCaption.appendChild( document.createTextNode("NBA :: Allstars Statistics-Table" )); var tHead = table.createTHead(); var addCell = function( row, label ) { return row.insertCell( -1 ).appendChild( document.createTextNode( label )); }; var xrow = tHead.insertRow( -1 ); for ( var i = 0; labels[ i ]; i++ ) { addCell( xrow, labels[ i ] ); } var tBody = document.createElement( "tbody" ); table.appendChild( tBody ); for ( i = 0; players[ i ]; i++ ) { xrow = tBody.insertRow( -1 ); for ( var info in players[ i ] ) { addCell( xrow, players[ i ][ info ] ); } } container.appendChild( table ); return; } alert( "unsupported features, please update your browser" ); return false; } // ]]> </script> </head> <body id="xhtml-10-transitional"> <div id="view"></div> <!-- id :: view --> </body> </html>
hope it helps...
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
Thanks,
•
•
•
•
I getting a better understanding of parsing throuhg objects. Quick question. Wy won't the following work. I'm getting undefined when I try and execute the code.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
for(var player in players) document.write(player["College"]);
•
•
•
•
Hi Gary,
here's your code, i've extrcted all of its collections with the used of a table, but if don't like my concept of using table to differentiate the items inside the players collections -- then simply re-apply the same procedure in my original post, using theinnerHTMLmethod.
All codes is as follows:
javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html id="xhtml10" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- W3Cs Standard Template : XHTML 1.0 Transitional DTD --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>www.daniweb.com</title> <style type="text/css"> /* <![CDATA[ */ table { border-collapse : collapse; border : 1px solid #C0C0C0; color : #365895; text-align : center; width : 100%; } caption { width : auto; text-align : left; margin-bottom : .500em; padding-left : .500em; } td { border-top : 1px solid #C0C0C0; border-right : 1px dashed #C0C0C0; padding : .350em; width : auto; vertical-align : middle; letter-spacing : 2px; } thead td { background-color : #F0FFFF; border-right : 1px dashed #C0C0C0; padding : .350em; font : bold 80%/1 "Trebuchet MS", Verdana, Arial, sans-serif; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ window.onload = function() { if ( document.createElement ) { var players = [ { PlayerName : "Ron Artest", Position : "Forward", Height : "6-7", Weight : 260, College : "St. Johns" }, { PlayerName : "Kobe Bryant", Position : "Guard", Height : "6-6", Weight : 205, College : "Lower Marion High Sc" } ]; var container = document.getElementById("view"); var labels = [ "Name", "Position", "Height", "Weight", "College" ]; var table = document.createElement("table"); var tCaption = table.createCaption(); tCaption.appendChild( document.createTextNode("NBA :: Allstars Statistics-Table" )); var tHead = table.createTHead(); var addCell = function( row, label ) { return row.insertCell( -1 ).appendChild( document.createTextNode( label )); }; var xrow = tHead.insertRow( -1 ); for ( var i = 0; labels[ i ]; i++ ) { addCell( xrow, labels[ i ] ); } var tBody = document.createElement( "tbody" ); table.appendChild( tBody ); for ( i = 0; players[ i ]; i++ ) { xrow = tBody.insertRow( -1 ); for ( var info in players[ i ] ) { addCell( xrow, players[ i ][ info ] ); } } container.appendChild( table ); return; } alert( "unsupported features, please update your browser" ); return false; } // ]]> </script> </head> <body id="xhtml-10-transitional"> <div id="view"></div> <!-- id :: view --> </body> </html>
hope it helps...
Gary,
You've forgot the s' over the ( object )
You've forgot the s' over the ( object )
players where it should be:for ( var player in players ) { document.write( players[ "College" ] );
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
Thanks Posting Shark,
Let's say instead of one object I get two objects back from the server the second object is the local address of the logo of the NBA team which I want to display in a div tag below the table with the players name. How would you go about doing this? I really appreciate all your help?
Let's say instead of one object I get two objects back from the server the second object is the local address of the logo of the NBA team which I want to display in a div tag below the table with the players name. How would you go about doing this? I really appreciate all your help?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
[ {"PlayerName":"Ron Artest","Position":"Forward","Height":"6-7","Weight":"260","College":"St. Johns"}, {"PlayerName":"Kobe Bryant","Position":"Guard","Height":"6-6","Weight":"205","College":"Lower Marion High Sc"} ][ {"PlayerName":"Ron Artest","Position":"Forward","Height":"6-7","Weight":"260","College":"St. Johns"}, {"PlayerName":"Kobe Bryant","Position":"Guard","Height":"6-6","Weight":"205","College":"Lower Marion High Sc"} ] [{"TeamLogo":"C:\\wamp\\wwwNBA\\laLakersLogo.gif"}]
•
•
•
•
Hi Gary,
here's your code, i've extrcted all of its collections with the used of a table, but if don't like my concept of using table to differentiate the items inside the players collections -- then simply re-apply the same procedure in my original post, using theinnerHTMLmethod.
All codes is as follows:
javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html id="xhtml10" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- W3Cs Standard Template : XHTML 1.0 Transitional DTD --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>www.daniweb.com</title> <style type="text/css"> /* <![CDATA[ */ table { border-collapse : collapse; border : 1px solid #C0C0C0; color : #365895; text-align : center; width : 100%; } caption { width : auto; text-align : left; margin-bottom : .500em; padding-left : .500em; } td { border-top : 1px solid #C0C0C0; border-right : 1px dashed #C0C0C0; padding : .350em; width : auto; vertical-align : middle; letter-spacing : 2px; } thead td { background-color : #F0FFFF; border-right : 1px dashed #C0C0C0; padding : .350em; font : bold 80%/1 "Trebuchet MS", Verdana, Arial, sans-serif; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ window.onload = function() { if ( document.createElement ) { var players = [ { PlayerName : "Ron Artest", Position : "Forward", Height : "6-7", Weight : 260, College : "St. Johns" }, { PlayerName : "Kobe Bryant", Position : "Guard", Height : "6-6", Weight : 205, College : "Lower Marion High Sc" } ]; var container = document.getElementById("view"); var labels = [ "Name", "Position", "Height", "Weight", "College" ]; var table = document.createElement("table"); var tCaption = table.createCaption(); tCaption.appendChild( document.createTextNode("NBA :: Allstars Statistics-Table" )); var tHead = table.createTHead(); var addCell = function( row, label ) { return row.insertCell( -1 ).appendChild( document.createTextNode( label )); }; var xrow = tHead.insertRow( -1 ); for ( var i = 0; labels[ i ]; i++ ) { addCell( xrow, labels[ i ] ); } var tBody = document.createElement( "tbody" ); table.appendChild( tBody ); for ( i = 0; players[ i ]; i++ ) { xrow = tBody.insertRow( -1 ); for ( var info in players[ i ] ) { addCell( xrow, players[ i ][ info ] ); } } container.appendChild( table ); return; } alert( "unsupported features, please update your browser" ); return false; } // ]]> </script> </head> <body id="xhtml-10-transitional"> <div id="view"></div> <!-- id :: view --> </body> </html>
hope it helps...
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
Posting Shark,
I made a mistake in my previous post and included three objects instead of two. Below is the correct number of objects
[
{"PlayerName":"Ron Artest","Position":"Forward","Height":"6-7","Weight":"260","College":"St. Johns"},
{"PlayerName":"Kobe Bryant","Position":"Guard","Height":"6-6","Weight":"205","College":"Lower Marion High Sc"}
]
[{"TeamLogo":"C:\\wamp\\wwwNBA\\laLakersLogo.gif"}]
I made a mistake in my previous post and included three objects instead of two. Below is the correct number of objects
[
{"PlayerName":"Ron Artest","Position":"Forward","Height":"6-7","Weight":"260","College":"St. Johns"},
{"PlayerName":"Kobe Bryant","Position":"Guard","Height":"6-6","Weight":"205","College":"Lower Marion High Sc"}
]
[{"TeamLogo":"C:\\wamp\\wwwNBA\\laLakersLogo.gif"}]
•
•
•
•
Thanks,
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
for(var player in players) document.write(player["College"]);
Hi,
it will be alot more easier if you will grouped them as a team, instead of defining another object for the path of images just to display each logo.
In this approch i've used the
you can easily expand all procedures inside this function. Just be careful when you edit the lines'.
it will be alot more easier if you will grouped them as a team, instead of defining another object for the path of images just to display each logo.
In this approch i've used the
document.writeln method, and created a table to differentiate each of the item inside the teams object variable. javascript Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <title>W3C HTML 4.01 loose DTD</title> <style type="text/css"> <!-- td { border-right : 1px solid #A0A0A0; border-top : 1px solid #A0A0A0; background-color : #F1F1F1; color : #405060; } th:first-child { border-bottom : 1px solid #A0A0A0; border-right : 1px solid #A0A0A0; background-color : #800090; color : yellow; width : 25%; } table { border : 1px solid #A0A0A0; border-collapse : collapse; text-align : left; width : 100%; } // --> </style> <script type="text/javascript"> <!-- var teams = [ { "LA Lakers" : { "Team Logo" : "C:\\wamp\\wwwNBA\\laLakersLogo.gif", Players : { 37 : { // Jersey n# PlayerName : "Ron Artest", Position : "Forward", Height : "6-7", Weight : "260", College : "St. Johns" }, 24 : { PlayerName : "Kobe Bryant", Position : "Guard", Height : "6-6", Weight : "205", College : "Lower Marion High Sc" } } } } // Add More NBA teams below >>> ]; // --> </script> </head> <body> <noscript> <p> This site requires a browser that support <b>JavaScript</b>. </p> </noscript> <div id="nbastats"> <script type="text/javascript"> <!-- ( function() { var tLen = teams.length; var table = '<table id="tableOne" border="0" cellpadding="5" cellspacing="0" frame="void" rules="none" summary="NBA Statistic Table">\n'; for ( var x = 0; x < tLen; x++ ) { table += "<tr>\n"; for ( var teamName in teams[ x ] ) { table += "<th title=\"" + teamName + "\" colspan=\"3\">Team Name: </th>\n"; table += "<td colspan=\"3\">" + teamName + "</td>\n"; table += "</tr>\n"; table += "<tr>\n"; for ( var misc in teams[ x ][ teamName ] ) { table += "<th colspan=\"3\">" + misc + "</th>\n"; } table += "</tr>\n"; table += "<tr>\n"; var count = 10; var image = new Image(); image.src = teams[ x ][ teamName ][ "Team Logo" ]; table += "<td style=\"background-color : #FFFFFF;\" rowspan=\"" + ( count += ( 1 * 10 )) + "\" colspan=\"3\"><img src=\"" + image.src + "\" alt=\"" + image.src + "\"></td>"; table += "</tr>\n"; for ( var j in teams[ x ][ teamName ][ misc ] ) { for ( var player in teams[ x ][ teamName ][ misc ][ j ] ) { table += "<tr>\n"; table += "<td style=\"text-align : center; background-color : #E0E0E0\">" + player + "</td><td>" + teams[ x ][ teamName ][ misc ][ j ][ player ] + "</td>\n"; table += "</tr>\n"; } table += "<tr>\n"; table += "<td>Jersey no.</td><td>#" + j + "</td>\n"; table += "</tr>\n"; table += "<tr>\n"; table += "<td colspan=\"2\" style=\"background-color : #800090; height : 5px;\"> </td>\n"; table += "</tr>\n"; } } } table += "</table>"; document.writeln( table ); } )( /* NBA Teams & Players */ ) // --> </script> </div> </body> </html>
you can easily expand all procedures inside this function. Just be careful when you edit the lines'.
Last edited by essential; Aug 24th, 2009 at 5:29 am.
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
![]() |
Similar Threads
- Very simple question in Javascript, please (JavaScript / DHTML / AJAX)
- Parse JavaScript Variable to a XSLT Variable (XML, XSLT and XPATH)
- Problem with Javascript in Iframe (JavaScript / DHTML / AJAX)
- javascript addField() problem (JavaScript / DHTML / AJAX)
- Date Expired? date expiration alert (JavaScript / DHTML / AJAX)
- Javascript help with getelementbyid (JavaScript / DHTML / AJAX)
- I need a little help with my button processing (javascript) (JavaScript / DHTML / AJAX)
- Collection object with flexgrid (Visual Basic 4 / 5 / 6)
- 'null' is null or not an object (JavaScript / DHTML / AJAX)
- Scope issue with javascript. (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: I want on mouse over image to happen like this . please provide the code
- Next Thread: Portable IDE for HTML/JavaScript
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate array automatically browser bug calendar captchaformproblem cart checkbox child class close codes createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine events explorer ext file firefox form forms getselection google gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump libcurl maps masterpage math media microsoft object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post programming progressbar prototype redirect regex runtime safari scale scriptlets scroll search security shopping size software sql text textarea toggle unicode web website windowsxp wysiwyg \n





