@@ -12,10 +12,11 @@ can include multiple types of items, including other arrays.
12
12
To create an array you can either use the object constructor or the literal declaration,
13
13
by assigning your variable a list of values right after the declaration.
14
14
15
- <javascript caption =" A simple array " >
15
+ ``` js
16
+ // A simple array
16
17
var myArray1 = new Array ( ' hello' , ' world' ); // with constructor
17
18
var myArray2 = [ ' hello' , ' world' ]; // literal declaration, the preferred way
18
- </ javascript >
19
+ ```
19
20
20
21
The literal declaration is preferred, see the
21
22
[ Google Coding Guidelines] ( http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Array_and_Object_literals )
@@ -24,175 +25,192 @@ for more information.
24
25
If you don't know your values yet, it is also possible to declare an empty Array, and
25
26
add elements either through functions or through accessing by index:
26
27
27
- <javascript caption =" Creating empty arrays and adding values " >
28
+
29
+ ``` js
30
+ // Creating empty arrays and adding values
28
31
var myArray = [];
29
32
30
33
myArray .push (' hello' ); // adds 'hello' on index 0
31
34
myArray .push (' world' ); // adds 'world' on index 1
32
35
myArray[2 ] = ' !' ; // adds '!' on index 2
33
- </ javascript >
36
+ ```
34
37
35
38
'push' is a function which adds an element on the end of the array and expands the array
36
39
respectively. You also can directly add items by index. Missing indices will be filled
37
40
with 'undefined';
38
41
39
- <javascript caption =" Leaving indices " >
42
+ ``` js
43
+ // Leaving indices
40
44
var myArray = [];
41
45
42
46
myArray[0 ] = ' hello' ;
43
47
myArray[1 ] = ' world' ;
44
48
myArray[3 ] = ' !' ;
45
49
46
50
console .log (myArray); // logs ['hello', 'world', undefined, '!'];
47
- </ javascript >
51
+ ```
48
52
49
53
So 'push' is far more safe, especially if you don't know the size of your
50
54
array yet. With the index you not only assign values to array items, but also
51
55
access those.
52
56
53
- <javascript caption =" Accessing array items by index " >
57
+ ``` js
58
+ // Accessing array items by index
54
59
var myArray = [ ' hello' , ' world' , ' !' ];
55
60
console .log (myArray[2 ]); // logs '!'
56
- </ javascript >
61
+ ```
57
62
58
63
### Array methods and properties
59
64
60
65
## length
61
66
62
67
The 'length' property is used to know the amount of items in your array.
63
68
64
- <javascript caption =" Length of an array " >
69
+ ``` js
70
+ // Length of an array
65
71
var myArray = [ ' hello' , ' world' , ' !' ];
66
72
console .log (myArray .length ); // logs 3
67
- </ javascript >
73
+ ```
68
74
69
75
You will need the length property for looping through an array:
70
76
71
- <javascript caption =" For loops and arrays - a classic " >
77
+ ``` js
78
+ // For loops and arrays - a classic
72
79
var myArray = [' hello' , ' world' , ' !' ];
73
80
for (var i = 0 ; i < myArray .length ; i = i + 1 ) {
74
81
console .log (myArray[i]);
75
82
}
76
- </ javascript >
83
+ ```
77
84
78
85
Except when you are using for ... in loops:
79
86
80
- <javascript caption"For loops and arrays - alternate method">
87
+ ``` js
88
+ // For loops and arrays - alternate method
81
89
var myArray = [' hello' , ' world' , ' !' ];
82
90
for (var i in myArray) {
83
91
console .log (myArray[i]);
84
92
}
85
- </ javascript >
93
+ ```
86
94
87
95
## concat
88
96
89
97
With 'concat', you can concatenate two or more arrays
90
98
91
- <javascript caption =" Concatenating Arrays " >
99
+ ``` js
100
+ // Concatenating Arrays
92
101
var myArray = [2 , 3 , 4 ];
93
102
var myOtherArray = [5 , 6 , 7 ];
94
103
var wholeArray = myArray .concat (myOtherArray); // [2, 3, 4, 5, 6, 7]
95
- </ javascript >
104
+ ```
96
105
97
106
## join
98
107
99
108
'join' creates a string representation of your array. It's parameter is as string
100
- which works as a seperator between elements (default is a comma);
109
+ which works as a separator between elements (default is a comma);
101
110
102
- <javascript caption =" Joining elements " >
111
+ ``` js
112
+ // Joining elements
103
113
var myArray = [' hello' , ' world' , ' !' ];
104
114
console .log (myArray .join (' ' )); // logs "hello world !";
105
115
console .log (myArray .join ()); // logs "hello,world,!"
106
116
console .log (myArray .join (' ' )); // logs "helloworld!"
107
117
console .log (myArray .join (' !!' )) // logs "hello!!world!!!!!";
108
- </ javascript >
118
+ ```
109
119
110
120
## pop
111
121
112
122
'pop' removes the last element of an array. It is the opposite method to 'push'
113
123
114
- <javascript caption =" pushing and popping " >
124
+ ``` js
125
+ // pushing and popping
115
126
var myArray = [];
116
127
myArray .push (0 ); // [ 0 ]
117
128
myArray .push (2 ); // [ 0 , 2 ]
118
129
myArray .push (7 ); // [ 0 , 2 , 7 ]
119
130
myArray .pop (); // [ 0 , 2 ]
120
- </ javascript >
131
+ ```
121
132
122
133
## reverse
123
134
124
135
As the name suggests, the elements of the array are in reverse order after calling
125
136
this method
126
137
127
- <javascript caption =" reverse " >
138
+ ``` js
139
+ // reverse
128
140
var myArray = [ ' world' , ' hello' ];
129
141
myArray .reverse (); // [ 'hello', 'world' ]
130
- </ javascript >
142
+ ```
131
143
132
144
## shift
133
145
134
146
Removes the first element of an array. With 'pop' and 'shift' you can recreate the
135
147
method of a [ queue] ( http://en.wikipedia.org/wiki/Queue_(data_structure) )
136
148
137
- <javascript caption =" queue with shift() and pop() " >
149
+ ``` js
150
+ // queue with shift() and pop()
138
151
var myArray = [];
139
152
myArray .push (0 ); // [ 0 ]
140
153
myArray .push (2 ); // [ 0 , 2 ]
141
154
myArray .push (7 ); // [ 0 , 2 , 7 ]
142
155
myArray .shift (); // [ 2 , 7 ]
143
- </ javascript >
156
+ ```
144
157
145
158
## slice
146
159
147
160
Extracts a part of the array and returns them in a new one. This method takes one
148
161
parameter, which is the starting index.
149
162
150
- <javascript caption =" slicing " >
163
+ ``` js
164
+ // slicing
151
165
var myArray = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ];
152
166
var newArray = myArray .slice (3 );
153
167
154
168
console .log (myArray); // [1, 2, 3, 4, 5, 6, 7, 8]
155
169
console .log (newArray); // [4, 5, 6, 7, 8]
156
- </ javascript >
170
+ ```
157
171
158
172
## splice
159
173
160
174
Removes a certain amount of elements and adds new ones at the given index. It takes
161
175
at least 3 parameters
162
176
163
- <javascript caption =" splice method " >
177
+ ``` js
178
+ // splice method
164
179
myArray .splice (idx, len, values, ... );
165
- </ javascript >
180
+ ```
166
181
167
182
* idx = the starting index
168
183
* len = the number of elements to remove
169
184
* values = the values which should be inserted at idx
170
185
171
186
For example:
172
187
173
- <javascript caption =" splice example " >
188
+ ``` js
189
+ // splice example
174
190
var myArray = [0 , 7 , 8 , 5 ];
175
191
myArray .splice (1 , 2 , 1 , 2 , 3 , 4 );
176
192
console .log (myArray); // [0, 1, 2, 3, 4, 5]
177
- </ javascript >
193
+ ```
178
194
179
195
## sort
180
196
181
197
Sorts an array. It takes one parameter, which is a comparing function. If this function is not
182
198
given, the array is sorted ascending
183
199
184
- <javascript caption =" sorting without comparing function " >
200
+ ``` js
201
+ // sorting without comparing function
185
202
var myArray = [3 , 4 , 6 , 1 ];
186
203
myArray .sort (); // 1, 3, 4, 6
187
- </ javascript >
204
+ ```
188
205
189
- <javascript caption =" sorting with comparing function " >
206
+ ``` js
207
+ // sorting with comparing function
190
208
function descending (a , b ) {
191
209
return b - a;
192
210
}
193
211
var myArray = [3 , 4 , 6 , 1 ];
194
212
myArray .sort (descending); // [6, 4, 3, 1]
195
- </ javascript >
213
+ ```
196
214
197
215
The return value of descending (for this example) is important. If the return value is
198
216
less than zero, the index of a is before b, and if it is greater than zero it's vice-versa.
@@ -202,12 +220,13 @@ If the return value is zero, the elements index is equal.
202
220
203
221
Inserts an element at the first position of the array
204
222
205
- <javascript caption =" unshift " >
223
+ ``` js
224
+ // unshift
206
225
var myArray = [];
207
226
myArray .unshift (0 ); // [ 0 ]
208
227
myArray .unshift (2 ); // [ 2 , 0 ]
209
228
myArray .unshift (7 ); // [ 7 , 2 , 0 ]
210
- </ javascript >
229
+ ```
211
230
212
231
## forEach
213
232
@@ -222,7 +241,8 @@ The function takes up to three arguments:
222
241
223
242
All of the are optional, but you will need at least the 'element' parameter in most cases.
224
243
225
- <javascript caption =" native forEach " >
244
+ ``` js
245
+ // native forEach
226
246
function printElement (elem ) {
227
247
console .log (elem);
228
248
}
@@ -239,4 +259,4 @@ myArray = [1, 2, 3, 4, 5];
239
259
myArray .forEach (printElement); // prints all elements to the console
240
260
myArray .forEach (printElementAndIndex); // prints "Index 0: 1" "Index 1: 2" "Index 2: 3" ...
241
261
myArray .forEach (negateElement); // myArray is now [-1, -2, -3, -4, -5]
242
- </ javascript >
262
+ ```
0 commit comments