Skip to content

Commit e9dee6c

Browse files
author
Stefan Baumgartner
committed
FEATURE: Reworked the whole entry, tried to find examples for every array function
1 parent beb3e62 commit e9dee6c

File tree

1 file changed

+175
-18
lines changed

1 file changed

+175
-18
lines changed

content/javascript-101/arrays.md

Lines changed: 175 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,195 @@
22
chapter : js101
33
section: 6
44
title: Arrays
5-
attribution: jQuery Fundamentals
5+
attribution: jQuery Fundamentals, Stefan Baumgartner (@ddprrt)
66
---
77
Arrays are zero-indexed lists of values. They are a handy way to store a set of
88
related items of the same type (such as strings), though in reality, an array
9-
can include multiple types of items, including other arrays.
9+
can include multiple types of items, including other arrays. Arrays in Javascript
10+
work bascially like in most other programming or scripting languages, although there
11+
are some subtle differences.
12+
13+
To create an array you can either use the object constructor or the literal declaration,
14+
by assign your variable a list of values right after the declaration.
1015

1116
<javascript caption="A simple array">
12-
var myArray = [ 'hello', 'world' ];
17+
var myArray1 = new Array( 'hello', 'world' ); // with constructor
18+
var myArray2 = [ 'hello', 'world' ]; // literal declaration, the prefered way
19+
</javascript>
20+
21+
If you don't know your values yet, it is also possible to declare an empty Array, and
22+
add elements either through functions or through accessing by index:
23+
24+
<javascript caption="Creating empty arrays and adding values">
25+
var myArray = []; // var myArray = new Array(); would also work
26+
27+
myArray.push('hello'); // adds 'hello' on index 0
28+
myArray.push('world'); // adds 'world' on index 1
29+
myArray[2] = '!'; // adds '!' on index 2
30+
</javascript>
31+
32+
'push' is a function which adds an element on the end of the array and expands the array
33+
respectively. You also can directly add items by index. Missing indices will be filled
34+
with 'undefined';
35+
36+
<javascript caption="Leaving indices">
37+
var myArray = [];
38+
39+
myArray[0] = 'hello';
40+
myArray[1] = 'world';
41+
myArray[3] = '!';
42+
43+
console.log(myArray); // logs ['hello', 'world', undefined, '!'];
1344
</javascript>
1445

46+
So 'push' is far more safe, especially if you don't know the size of your
47+
array yet. With the index you not only assign values to array items, but also
48+
access those.
49+
1550
<javascript caption="Accessing array items by index">
16-
var myArray = [ 'hello', 'world', 'foo', 'bar' ];
17-
console.log(myArray[3]); // logs 'bar'
51+
var myArray = [ 'hello', 'world', '!'];
52+
console.log(myArray[2]); // logs '!'
1853
</javascript>
1954

20-
<javascript caption="Testing the size of an array">
21-
var myArray = [ 'hello', 'world' ];
22-
console.log(myArray.length); // logs 2
55+
### Array methods and properties
56+
57+
## length
58+
59+
The 'length' property is used to know the amount of items in your array.
60+
61+
<javascript caption="Length of an array">
62+
var myArray = [ 'hello', 'world', '!'];
63+
console.log(myArray.length); // logs 3
2364
</javascript>
2465

25-
<javascript caption="Changing the value of an array item">
26-
var myArray = [ 'hello', 'world' ];
27-
myArray[1] = 'changed';
66+
<javascript caption="For loops and arrays - a classic">
67+
var myArray = ['hello', 'world', '!'];
68+
for(var i = 0; i < myArray.length; i = i + 1) {
69+
console.log(myArray[i]);
70+
}
2871
</javascript>
2972

30-
<javascript caption="Adding elements to an array">
31-
var myArray = [ 'hello', 'world' ];
32-
myArray.push('new');
73+
## concat
74+
75+
With 'concat', you can concatenate two or more arrays
76+
77+
<javascript caption="Concatenating Arrays">
78+
var myArray = [2, 3, 4];
79+
var myOtherArray = [5, 6, 7];
80+
var wholeArray = myArray.concat(myOtherArray); // [2, 3, 4, 5, 6, 7]
3381
</javascript>
3482

35-
<javascript caption="Working with arrays">
36-
var myArray = [ 'h', 'e', 'l', 'l', 'o' ];
37-
var myString = myArray.join(''); // 'hello'
38-
var mySplit = myString.split(''); // [ 'h', 'e', 'l', 'l', 'o' ]
83+
## join
84+
85+
'join' creates a string representation of your array. It's parameter is as string
86+
which works as a seperator between elements (default is a comma);
87+
88+
<javascript caption="Joining elements">
89+
var myArray = ['hello', 'world', '!'];
90+
console.log(myArray.join(' ')); // logs "hello world !";
91+
console.log(myArray.join()); // logs "hello,world,!"
92+
console.log(myArray.join('')); // logs "helloworld!"
93+
console.log(myArray.join('!!')) // logs "hello!!world!!!!!";
3994
</javascript>
95+
96+
## pop
97+
98+
'pop' removes the last element of an array. It is the opposite method to 'push'
99+
100+
<javascript caption="pushing and popping">
101+
var myArray = [];
102+
myArray.push(0); // [ 0 ]
103+
myArray.push(2); // [ 0 , 2 ]
104+
myArray.push(7); // [ 0 , 2 , 7 ]
105+
myArray.pop(); // [ 0 , 2 ]
106+
</javascript>
107+
108+
## reverse
109+
110+
As the name suggests, the elements of the array are in reverse order after calling
111+
this method
112+
113+
<javascript caption="reverse">
114+
var myArray = [ 'world' , 'hello' ];
115+
myArray.reverse(); // [ 'hello', 'world' ]
116+
</javascript>
117+
118+
## shift
119+
120+
Removes the first element of an array. With 'pop' and 'shift' you can recreate the
121+
method of a [queue](http://en.wikipedia.org/wiki/Queue_(data_structure))
122+
123+
<javascript caption="queue with shift() and pop()">
124+
var myArray = [];
125+
myArray.push(0); // [ 0 ]
126+
myArray.push(2); // [ 0 , 2 ]
127+
myArray.push(7); // [ 0 , 2 , 7 ]
128+
myArray.shift(); // [ 2 , 7 ]
129+
</javascript>
130+
131+
## slice
132+
133+
Extracts a part of the array and returns them in a new one. This method takes one
134+
parameter, which is the starting index.
135+
136+
<javascript caption="slicing">
137+
var myArray = [1, 2, 3, 4, 5, 6, 7, 8];
138+
var newArray = myArray.slice(3);
139+
140+
console.log(myArray); // [1, 2, 3, 4, 5, 6, 7, 8]
141+
console.log(newArray); // [4, 5, 6, 7, 8]
142+
</javascript>
143+
144+
## splice
145+
146+
Removes a certain amount of elements and adds new ones at the given index. It takes
147+
at least 3 parameters
148+
149+
<javascript caption="splice method">
150+
myArray.splice(idx, len, values, ...);
151+
</javascript>
152+
153+
* idx = the starting index
154+
* len = the number of elements to remove
155+
* values = the values which should be inserted at idx
156+
157+
For example:
158+
159+
<javascript caption="splice example">
160+
var myArray = [0, 7, 8, 5];
161+
myArray.splice(1, 2, 1, 2, 3, 4);
162+
console.log(myArray); // [0, 1, 2, 3, 4, 5]
163+
</javascript>
164+
165+
## sort
166+
167+
Sorts an array. It takes one parameter, which is a comparing function. If this function is not
168+
given, the array is sorted ascending
169+
170+
<javascript caption="sorting without comparing function">
171+
var myArray = [3, 4, 6, 1];
172+
myArray.sort(); // 1, 3, 4, 6
173+
</javascript>
174+
175+
<javascript caption="sorting with comparing function">
176+
function descending(a, b) {
177+
return b - a;
178+
}
179+
var myArray = [3, 4, 6, 1];
180+
myArray.sort(descending); // [6, 4, 3, 1]
181+
</javascript>
182+
183+
The return value of descending (for this example) is important. If the return value is
184+
less than zero, the index of a is before b, and if it is greater than zero it's vice-versa.
185+
If the return value is zero, the elements index is equal.
186+
187+
## unshift
188+
189+
Inserts an element at the first position of the array
190+
191+
<javascript caption="unshift">
192+
var myArray = [];
193+
myArray.unshift(0); // [ 0 ]
194+
myArray.unshift(2); // [ 2 , 0 ]
195+
myArray.unshift(7); // [ 7 , 2 , 0 ]
196+
</javascript>

0 commit comments

Comments
 (0)