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