-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays-and-objects.html
197 lines (173 loc) · 5.12 KB
/
arrays-and-objects.html
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
195
196
197
<!DOCTYPE html>
<html>
<body>
<h1>
Arrays And Objects
</h1>
<h2>
The few differences between arrays and objects that shows when should Arrays be used and when should Objects be used
</h2>
<hr>
<h4> Length </h4>
<p>Determining the length of an Array is done by using .length, determining the length of an object however needs a different approach as we need to transform it into an array first by parsing the keys; <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys">more details here</a></p>
<p>
Code:
<pre>
var myArr = [1,2,3,4,5];
var myObj = {
0: 1,
1: 2,
2: 3,
3: 4,
4: 5
};
console.log('Length() - myArr',myArr.length);
console.log('length() - myObj',Object.keys(myObj).length);
</pre>
</p>
<hr>
<h4> Index Pushing </h4>
<p>If we want to add data directly to specific indexes in our array that will mess up the length of that array, objects however are able to handle that well enough</p>
<p>
Code:
<pre>
var myArr = [];
var myObj = {};
myArr[0] = 'something';
myArr[5] = 'something else';
// myArr.length should be 2 but instead it's actually 6 as it automatically adds 4 empty cells
console.log('indexPushing() - myArr',myArr,myArr.length);
myObj[0] = 'something';
myObj[5] = 'something else';
// That's the same as writing
/*
myObj = {
0: 'something',
5: 'something else';
}
*/
console.log('indexPushing() - myObj',myObj,Object.keys(myObj).length);
</pre>
</p>
<hr>
<h4> Associative Indexes </h4>
<p>When using associatives with arrays, the array's length doesn't pick those up as it only counts the numerical indexes, objects, however, because they are transformed into an array using their keys they are transformed into a numerical array and thus the length works</p>
<p>
Code:
<pre>
var myArr = [];
var myObj = {};
myArr['assoc1'] = 'something';
myArr['assoc2'] = 'something else';
// Because the length of an array is calculated by its numerical indexes the length of this array will be 0
console.log('associativeIndexes() - myArr',myArr,myArr.length);
myObj['assoc1'] = 'something';
myObj['assoc2'] = 'something else';
console.log('associativeIndexes() - myObj',myObj,Object.keys(myObj),Object.keys(myObj).length);
</pre>
</p>
<hr>
<h4> Looping </h4>
<p>Using a forEach loop on an associative array wouldn't work, however, with an object that got transformed into an array of keys it would</p>
<p>
Code:
<pre>
var myArrIndex = [1,2,3,4,5];
var myArrAssoc = [];
myArrAssoc['assoc1'] = 'something';
myArrAssoc['assoc2'] = 'something else';
var myObj = {
0: 1,
1: 2,
myassoc1: 3,
3: 4,
4: 5,
myassoc2: 'something'
};
// This will work
myArrIndex.forEach(function(value){
console.log('looping() - myArrIndex',value);
});
// This won't work
myArrAssoc.forEach(function(value){
console.log('looping() - myArrAssoc',value);
});
Object.keys(myObj).forEach(function(key){
console.log('looping() - myObj',myObj[key]);
});
</pre>
</p>
</body>
<script>
function length(){
var myArr = [1,2,3,4,5];
var myObj = {
0: 1,
1: 2,
2: 3,
3: 4,
4: 5
};
console.log('Length() - myArr',myArr.length);
console.log('length() - myObj',Object.keys(myObj).length);
}
length();
function indexPushing(){
var myArr = [];
var myObj = {};
myArr[0] = 'something';
myArr[5] = 'something else';
// myArr.length should be 2 but instead it's actually 6 as it automatically adds 4 empty cells
console.log('indexPushing() - myArr',myArr,myArr.length);
myObj[0] = 'something';
myObj[5] = 'something else';
// That's the same as writing
/*
myObj = {
0: 'something',
5: 'something else';
}
*/
console.log('indexPushing() - myObj',myObj,Object.keys(myObj).length);
}
indexPushing();
function associativeIndexes(){
var myArr = [];
var myObj = {};
myArr['assoc1'] = 'something';
myArr['assoc2'] = 'something else';
// Because the length of an array is calculated by its numerical indexes the length of this array will be 0
console.log('associativeIndexes() - myArr',myArr,myArr.length);
myObj['assoc1'] = 'something';
myObj['assoc2'] = 'something else';
console.log('associativeIndexes() - myObj',myObj,Object.keys(myObj),Object.keys(myObj).length);
}
associativeIndexes();
function looping(){
var myArrIndex = [1,2,3,4,5];
var myArrAssoc = [];
myArrAssoc['assoc1'] = 'something';
myArrAssoc['assoc2'] = 'something else';
var myObj = {
0: 1,
1: 2,
myassoc1: 3,
3: 4,
4: 5,
myassoc2: 'something'
};
// This will work
myArrIndex.forEach(function(value){
console.log('looping() - myArrIndex',value);
});
// This won't work
myArrAssoc.forEach(function(value){
console.log('looping() - myArrAssoc',value);
});
Object.keys(myObj).forEach(function(key){
console.log('looping() - myObj',myObj[key]);
});
}
looping();
</script>
</html>