Data Structures: Objects and Arrays - TristanVarewijck/Block-Tech GitHub Wiki
Data Sets
Instead of putting a long 'string' with data JavaScript provides a data type specifically for storing sequences of values called an array.
Example: let groupMembers = ['John', 'Lisa', 'Juan', 'Bella'];
A group of values in an array. We can call individual items in the array by doing this:
> groupMembers[2] = 'Juan'
An array works just like a normal string with indexes, but what if we want the second letter in Bella's name..? We can do this:
> groupMembers[3][1] = 'e'
Properties
Properties can give additional value.
let myName = 'Tristan';
> myName.length = 7;
The .length property give your the numbers of characters in a string. Almost every value in JavaScript can have properties the exceptions are null and undefined.
But how can we acces a property? We can do this by the .dot notation for example .length or with the square brackets[]. Some properties only work with the dot notation or are just easier to write like that: .length is easier than ['length'];
Methods
Properties that contain value's are generally called methods / .length doesn't contain a value because it works only 1 way. This example will show you how a method works to manipulatie array's:
let groupMembers = ['John', 'Lisa', 'Juan', 'Bella'];
> groupMembers.push('Tristan');
> groupMembers <br>
_['John', 'Lisa', 'Juan', 'Bella', 'Tristan']_
With the .push method i can add a value at the end of an array by giving this method a value: .push('Tristan');
Objects
With objects we can give more values to a variable by grouping objects and saving them to a 'key'. With objects we can for example make profiles for a group of people with nested objects. By doing this we store more value or meaning to a variable. We can actually store multiple array's as well.
Example:
> let france = {
president: 'Emmanuel Macron',
population: 66990000,
capital: 'Paris',
famousCitys: ['Paris', 'Nice', 'Marseille', 'Bordeaux'],
countryCode: 33,
};
By making this object we actually created a profile of France. but what if i want to acces the value 'Bordeaux' in this object how can we do this?
> france[famousCitys][3]; = 'Bordeaux'
Strings and their Properties
"Values of type string, number, and Boolean are not objects, and though the language doesn’t complain if you try to set new properties on them, it doesn’t actually store those properties. As mentioned earlier, such values are immutable and cannot be changed."
For example:
let myName = 'Tristan';
myName.age = 21;
> console.log(myName.age); // undefined
Every string value has a number of methods. Some very useful ones are slice and indexOf
> console.log('Tristan'.indexOf('s')); // 3 <br>
> console.log('Tristan'.slice(3, 7)); // stan
json
"If you want to save data in a file for later or send it to another computer over the network, you have to somehow convert these tangles of memory addresses to a description that can be stored or sent."
What we can do is serialize the data. That means it is converted into a flat description. A populair format in wich we can do this is a json file what stands for: Javascript Object Notation. A jason file looks a lot like normal Javascript but it has some restrictions...
- All property names have to be surrounded by double quotes
- only simple data expressions are allowed—no function calls, bindings, or anything that involves actual computation
- Comments are not allowed in JSON
Resource
- Objects and Arrays | Eloquent JavaScript 3rd edition (2018)