-
Notifications
You must be signed in to change notification settings - Fork 482
/
data-structures.js
194 lines (155 loc) · 4.8 KB
/
data-structures.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict'
// Data structures allow us to structure and organise multiple primitive data
/*
Object
Objects are the most common data structure in JavaScript
It is the base to all other data structures
They hold properties
A property is a key / value pair
The key is a string and we associate it to a value, very much like variables
We use the curly braces `{}` to create an object
*/
console.log({}) // Loggin an empty object
// To create a property we use the colon `:`
// The first string is a key, and after the colon is the value
console.log({ 'awesome key': 'great value' })
// here I set the string 'great value' to the key 'awesome key'
// I repeate, keys are always strings
// Now, the true power of objects lies in storing multiple properties in them
// To do that we just separate properties with a comma `,` like so :
console.log({ 'first key': 1, 'second key': 22 })
// here I set the number 1 to the key 'first key'
// and the number 22 to 'second key'
// for big objects we split them into multiple lines, one per property
console.log({
'first key': 1,
'second key': 22,
'third key': 333,
'fourth key': 4444,
'the last key': 'the end',
})
// If our key is a valid identifiers (like variables identifiers)
// We can omit the quotes ''
console.log({ validKey: 'valid value !' })
// Since this syntax is lighter it is prefered in JavaScript
// so try to use valid identifiers and camelCase whenever possible
// (same rules as variabes)
// Ok let's see how we can model usefull data with that
// here's an address
console.log({
country: 'France',
town: 'Toulouse',
postalCode: 31000,
streetNumber: 175,
streetLabel: 'boulevard',
streetName: 'Matabiau',
})
// Now since objects are values, we can nest them !
// let's refactor a bit our previous object to avoid repetition in the keys
console.log({
country: 'France',
town: 'Toulouse',
postalCode: 31000,
street: {
number: 175,
label: 'boulevard',
name: 'Matabiau',
},
})
// And like all values, we can store them in variables
const myStreet = {
number: 175,
label: 'boulevard',
name: 'Matabiau',
}
// sweet, let's log that variable value
console.log(myStreet)
// Guess what, we can use variables values when creating objects !
console.log({
country: 'France',
town: 'Toulouse',
postalCode: 31000,
street: myStreet, // here, the value contained in myStreet is used
})
// Let's store this address into a variable
const address = {
country: 'France',
town: 'Toulouse',
postalCode: 31000,
street: myStreet,
}
// Now that would be a more complete user object :
console.log({
firstname: 'Clement',
lastname: 'Denis',
email: 'cdenis@thot.space',
age: 29,
address: address,
})
// If we use a variable as a property, like here in address
// We can omit the key, JavaScript will use the variable identifier as it's key
// So we can rewrite the previous user object like so :
console.log({
firstname: 'Clement',
lastname: 'Denis',
email: 'cdenis@thot.space',
age: 29,
address, // look ma, no key !
})
// Of course this works only when our variable identifier is our key.
// That's it on declaring objects !
/*
Array
They are used to keep ordered lists of values.
In JavaScript, Arrays are Objects with special rules:
- They use number as keys, we call those keys index (starting at 0)
- They keep their content in order
- They have a special property 'length' to get the size of our list
- And much, much more (but that's enough for now)
We use the square brackets `[]` to create an Array
*/
console.log([]) // Here I log an empty array
console.log([ 1, 2, 3 ]) // We don't need to specify the index
// that would roughly translate to this object:
console.log({ '0': 1, '1': 2, '2': 3 })
// So if we have a bunch of values we want to group together
// but the keys are not important, we can use an array
// to automaticaly create ordered index for our values.
// Overall the syntax to create them is quite simpler than objects
// since we don't need to specify the keys.
// Just like objects, they are values
// So like all other values we can store them in variables
const allowedCountries = [
'France',
'Spain',
'Portugal',
'Russia',
'Iceland',
]
console.log(allowedCountries)
// Of course we can make arrays of arrays
console.log([
[ 32, 45 ],
[ -38, 57 ],
[ 87, 99 ],
[ 57, -2 ],
[ -74, -29 ],
])
// we can use variables values
console.log([ allowedCountries, allowedCountries, allowedCountries ])
// Here I made an array with 3 countries
// And we can freely mix arrays and objects, any values really, surprise me
console.log({
head: [
{ x: 5, y: 5 },
{ x: 6, y: 4 },
{ x: 7, y: 3 },
],
tail: [
{ x: 3, y: 7 },
{ x: 4, y: 6 },
{ x: 5, y: 5 },
],
})
// This way we can model our data to have a structure that
// help us understand it and access it