Array
Array typically consist of number, string, boolean, or null values and can also include nested arrays and nested objects. An array's elements are comma-delimited and are contained in brackets.
var newArray = [ "Text goes here", 29, true, null ]
var otherArray = [ [...], {...} ]
console.log( newArray[2] ); trueArray with objects
This array consists of comma-delimited, nested objects which ceach ontain multiple key/value pairs- also comma-delimited. These objects can be accessed using the array's variable name and an index.
var newArray = [
{ "name": "Dagny Taggart", "age": 39 },
{ "name": "Francisco D'Anconia", "age": 40 },
{ "name": "Hank Rearden", "age": 46 }
]
console.log( newArray[0].name ); Dagny TaggartObject
This object consists of several key/value pairs. These 'properties' can be accessed using the the object's variable name followed by a period and property name -or- can be accessed like an array using the property name in quotes (in place of an index).
newObject = {
"first": "Jimmy",
"last": "James",
"age": 29,
"sex": "M",
"salary": 63000,
"registered": false
}
console.log( newObject.salary ); 63000 63000Object with nested array
This object contains comma-delimited key/value pairs and a nested array. The nested array can be accessed with the object name, property or 'key' containing the array and an array index.
newObject = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 70000,
"registered": true,
"interests": [ "Reading", "Mountain Biking", "Hacking" ]
}
console.log( newObject.interests[0] ); Reading ReadingObject with nested object
This object contains multiple comma-delimited key/value pairs and a nested object. To access an object within an object, property names or 'keys' can be chained together -or- property names can be used like an array in place of indexes.
newObject = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 70000,
"registered": true,
"favorites": {
"color": "Blue",
"sport": "Soccer",
"food": "Spaghetti"
}
}
console.log( newObject.favorites.food ); Spaghetti SpaghettiObject with nested arrays and objects
This object is more complex and typical of what might come from an API. It contains key/value pairs, nested arrays and nested objects. Any of its elements can be accessed with a combination of the above techniques.
newObject = {
"first": "John",
"last": "Doe",
"age": 39,
"sex": "M",
"salary": 70000,
"registered": true,
"interests": [ "Reading", "Mountain Biking", "Hacking" ],
"favorites": {
"color": "Blue",
"sport": "Soccer",
"food": "Spaghetti"
},
"skills": [
{
"category": "PHP",
"tests": [
{ "name": "One", "score": 90 },
{ "name": "Two", "score": 96 }
]
},
{
"category": "CouchDB",
"tests": [
{ "name": "One", "score": 32 },
{ "name": "Two", "score": 84 }
]
},
{
"category": "Node.js",
"tests": [
{ "name": "One", "score": 97 },
{ "name": "Two", "score": 93 }
]
}
]
}
console.log( newObject.skills[0].category ); PHP PHP 32 32