Skip to content

Instantly share code, notes, and snippets.

@joechan3
Last active April 23, 2016 17:18
Show Gist options
  • Save joechan3/2c292c292d1369db06c95915391e5d1b to your computer and use it in GitHub Desktop.
Save joechan3/2c292c292d1369db06c95915391e5d1b to your computer and use it in GitHub Desktop.
Accessing objects in arrays From: Lynda - JavaScript and JSON
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>JavaScript And JSON</title>
</head>
<body>
<h2>Links</h2>
<ol id="links">
</ol>
<script>
var info = {
"full_name" : "Ray Villalobos",
"title" : "Staff Author",
"links" : [
{ "blog" : "http://iviewsource.com" },
{ "facebook" : "http://facebook.com/iviewsource" },
{ "podcast" : "http://feeds.feedburner.com/authoredcontent" },
{ "twitter" : "http://twitter.com/planetoftheweb" },
{ "youtube" : "http://www.youtube.com/planetoftheweb" }
]
};
var output = '';
for (var i = 0; i <= info.links.length; i++) {
for (key in info.links[i]) {
if (info.links[i].hasOwnProperty(key)) {
output += '<li>' +
'<a href = "' + info.links[i][key] +
'">' + key + '</a>' +
'</li>';
} // hasOwnProperty check
} // for each object
} //for each array element
var update = document.getElementById('links');
update.innerHTML = output;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment