Data Types - Objects
Objects, like arrays, are lists of elements. But unlike arrays, objects are not ordered. Instead of identifying elements by index, we identify them by key. So an object is a list of key / value pairs separated by colon (:).
Objects have to be declared inside curly braces { }.
var employee = {
'name': 'James Taylor',
'yearOfBirth': 1948,
'ID': 'SBJ0001',
'role': 'IT Analyst'
};
Objects keys are also called properties. To access the properties we can use braces or the dot notation:
console.log( employee['ID'] ); // The console will show 'SBJ0001'
console.log( employee.ID ); // Same thing using the dot notation
Important: the dot notation only works with properties that follow the variables naming rules.
var test = {
'property1': 'Some value',
'2a': 'Some other value',
09335: 'Another value',
'hello-world': 'Last value'
};
// 'property1' can be retrieved using the dot notation:
console.log( test.property1 ); // The console will show 'Some value'
// The other ones can't because they don't follow the naming rules. They can only be retrieved with braces.
console.log( test['hello-world'] ); // The console will show 'Last value'
We can alter values assigned to properties and add new properties.
var employee = {
'name': 'James Taylor',
'yearOfBirth': 1948,
'ID': 'SBJ0001',
'role': 'IT Analyst'
};
employee.role = 'IT Manager'; // alter a value
employee.passport = 'KV09888'; // add a new property
console.log(employee);
/* The console will show:
{
'name': 'James Taylor',
'yearOfBirth': 1948,
'ID': 'SBJ0001',
'role': 'IT Manager',
'passport': 'KV09888'
};
*/
From now on you'll commonly work with multidimensional arrays and objects mixed together. We just need to pay attention to their structure (braces and curly braces) and use the correct indentation so it's easy to interpret them.
var courses = [
{
'title': 'Learn code in Python 3',
'reviews': 6802,
'students': 130129,
'categories': ['programming', 'technology']
},
{
'title': 'Learn PHP - Beginner to Advanced',
'reviews': 1204,
'students': 30521,
'categories': ['web development', 'programming']
},
{
'title': 'Learn Microsoft Excel 2020',
'reviews': 4209,
'students': 18560,
'categories': ['productivity', 'business']
}
];
Some other aspects worth remembering:
- Arrays are identified by braces [ ].
- Arrays are ordered lists of values separated by comma.
- Objects are identified by curly braces { }.
- Objects elements are key / value pairs separated by colon.
- Objects elements are separated by comma.
Therefore we can conclude the following:
- The courses variable is an array containing 3 elements.
- The elements of courses array are objects.
- Each of these objects has 4 elements
- The objects have values of different data types. Strings ('title'), numbers ('reviews' and 'students') and arrays ('categories')
How do we get to the 'web development' category of the PHP course then?
console.log( courses[1]['categories'][0] );
We can use this 'path' to alter the value:
courses[1]['categories'][0] = 'full stack web development';
console.log( courses[1]['categories'][0] ); // The console will show ['full stack web development', 'programming']