Arrays and Objects are the hallmarks of many programming languages.
IMPORTANT NOTE: It is important to remember that in most programming languages:
Legend for list below: S = String, N = Number, B = Boolean, O = Object, A = Array, K = Key, V = Value
An array is a special variable container for an "array" of MULTIPLE data types that is created by using a set of square brackets with each array ELEMENT separated by commas inside of it.
<script> window.onload = function(){
/* An Array --------------------------- */
var myFamilyArray = ["Cornelius", "Sheila", "Joshua", "Josiah"]
console.log(myFamilyArray); } // end of window.onload function
</script>
</head>
You access an array by its INDEX NUMBER.
console.log(myFamilyArray); console.log(myFamilyArray[1]);
</script>
Because an array can contain any data type, it is common to have an array contain other arrays. This is known as an ARRAY OF ARRAYS or a multi-dimensional array.
console.log(myFamilyArray[1]);
/* An Array of Arrays --------------------------- */
var myFamilyArrayOfArrays = [["Cornelius", "Instructor"],["Sheila", "Manager"],["Joshua", "Student"],["Josiah", "Student"]] console.log(myFamilyArrayOfArrays);
You access an array of arrays by its INDEX NUMBER of BOTH of the arrays.
console.log(myFamilyArrayOfArrays);
console.log(myFamilyArrayOfArrays[0][1]);
While arrays use square brackets ([....]), objects use curly braces ({.....}) with name/value pairs separated by colons. This is called a JavaScript object.
console.log(myFamilyArrayOfArrays[0][1]); /* An Object -------------------- */
var myEmployee = {firstName: "Cornelius",
lastName: "Chopin", title: "Instructor"
};
console.log(myEmployee);
You access an object by its PROPERTY NAME instead of an index number.
console.log(myEmployee);
console.log(myEmployee.firstName);
Like arrays, you can put an object or objects in an object.
<h1>Arrays and Objects Examples</h1> <h2 id="object_in_object"></h2>
console.log(myEmployee.firstName); myObject = {
"name":"Cornelius",
"title":"App Developer",
"apps": {
"app1":"Chopinisms",
"app2":"Thinking Inside The Box",
"app3":"Learning Scriptures Made Easy"
}
}
console.log(myObject);
document.getElementById("object_in_object").innerHTML += myObject.apps.app2 + "<br>";
// or
document.getElementById("object_in_object").innerHTML += myObject.apps["app2"];
document.getElementById("object_in_object").innerHTML += myObject.apps["app2"];
console.log(myObject.apps.app3);
Once you have learned how to create an array and an object and how to acess an array of arrays, let's turn our attention to a common technique that is a hybrid of the two types: An ARRAY OF OBJECTS.
/* An Array of Objects --------------------------- */ var myFamilyArrayOfObjects = [
{name:"Cornelius", title:"Instructor", gender:"male"},
{name: "Sheila", title:"Manager", gender:"female"},
{name: "Joshua", title:"Student", gender:"male"},
{name:"Josiah", title:"Student", gender:"male"} ] console.log(myFamilyArrayOfObjects);
You access an array of objects by the array INDEX by a access operator square brackets syntax AND the object PROPERTY by the dot syntax (object.property).
console.log(myFamilyArrayOfObjects); console.log(myFamilyArrayOfObjects[3].name);
A JSON (JavaScript Object Notation) Object is very much similar to a JavaScript object; however, it differs in that it is a text string. You can parse an JSON Object and then "write" it to a page.
<h2 id="object_in_object"></h2>
<p id="json_object"></p>
console.log(myObject.apps.app2); /* Parsing A JSON OBJECT -------------------- */
var myJSON = '{ "name":"Cornelius", "title":"Instructor", "city":"Rock Rock" }';
var myObj = JSON.parse(myJSON);
console.log(myJSON);
document.getElementById("json_object").innerHTML = "<strong>Name: </strong>" + myObj.name + "<br/>" + "<strong>City: </strong>" + myObj.city + "<br/>" + "<strong>Title: </strong>" + myObj.title;
Write the following "p" tag in the body of the document below the last h1 tag:
<p id="json_object"></p> <p id="xml_object"></p>
document.getElementById("json_object").innerHTML =
"<strong>Name: </strong>" + myObj.name + "<br/>" +
"<strong>City: </strong>" + myObj.city + "<br/>" +
"<strong>Title: </strong>" + myObj.title; /* Parsing An XML OBJECT ---------------------- */ var text = "<employees><employee>" +
"<name>Cornelius Chopin</name>" +
"<title>Accessibility Specialist</title>" +
"<start_date>June 22, 2012</start_date>" +
"</employee></employees>"; var parser = new DOMParser();
var xmlDoc = parser.parseFromString(text,"text/xml"); document.getElementById("xml_object").innerHTML =
"<strong>Name:</strong> " + xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue + "<br/>" +
"<strong>Title: </strong>" + xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue + "<br/>" +
"<strong>Start Date: </strong>" + xmlDoc.getElementsByTagName("start_date")[0].childNodes[0].nodeValue;