-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays-vs-objects.js
More file actions
162 lines (115 loc) · 3.36 KB
/
arrays-vs-objects.js
File metadata and controls
162 lines (115 loc) · 3.36 KB
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
// objects:
var obj = {
'foo': 1,
'bar': 2,
'baz asdfadsfadsf': 3
};
var book = {
author: 'JK Rowling',
title: 'Harry Potter'
};
obj.foo; // => 1
obj.baz; // => 1
// you know exactly what key you want.
// arrays:
var arr = ['foo', 'bar', 'baz', 'roo', 'asdf'];
arr[0]; // => 'foo'
// is kind of like
var obj = {
0: 'foo',
1: 'bar',
2: 'baz',
3: 'roo',
4: 'asdf'
};
obj['1'] === arr[1];
obj['3'] === arr[3];
// ====================================
// Arrays
// ====================================
// arrays are indexed by a number, so:
// - you can do math on the indices
// - you can loop on indices
// - you can use array methods, like sort
// - you can manipulate strings like an array
// ** you can do math on the indices **
arr[arr.length - 1]; // returns the last element
arr[Math.floor(arr.length / 2)]; // tries to return the middle element
// ** you can loop on indices **
// example: print every other element in the collection
// -- array
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
// ------ equivalent shortcut: forEach
arr.forEach(function(element, i, wholeArray) {
console.log(arr[i]);
});
// -- object
console.log(obj.1);
console.log(obj.3);
console.log(obj.5);
// ** you can use array methods, like sort **
arr.sort(); // 123 or "abc" (unicode sort)
arr.sort(function(a, b) {
return a - b;
});
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
// #push: adds to the end
var arr = [1,2,3,4];
arr.push(0);
console.log(arr); // => [1,2,3,4,0]
// #unshift: adds to the beginning
var arr = [1,2,3,4];
arr.unshift(0);
console.log(arr); // => [0,1,2,3,4]
// #pop: removes the last (mutates the array)
var arr = [1,2,3,4];
arr.pop();
console.log(arr); // => [1,2,3]
// #slice: returns a COPY of a slice of the array,
// slice([startIndex], [length])
var arr = [1,2,3,4];
arr.slice(0, 2);
console.log(arr); // => [1,2,3,4] because original arr not affectd
console.log(arr.slice(1,2)); // => [2,3] prints the COPY
// #length
var arr = [1,2,3,4];
console.log(arr.length); // => 4
// #concat takes an ARRAY and returns a COPY of the merger of both arrays
// #concat takes an ARRAY as the argument
var arr = [1,2,3,4];
arr.concat(['a','b','c']);
console.log(arr); // => [1,2,3,4]; arr not affected by #concat
console.log(arr.concat(['a','b','c'])); // => [1,2,3,4,'a','b','c']
// #indexOf
var arr = ['a','b','c'];
arr.indexOf('b'); // => 1
// ** deletion (works on both objects and arrays) **
var arr = ['a','b','c'];
delete arr[1];
console.log(arr); // => ['a', 'c']
var obj = {
foo: 'bar'
};
delete obj.foo;
console.log(obj); // => {}
// exercise example: find all the vowels of a string
// use delete and #indexOf
// ** you can manipulate strings like an array **
var string = 'hooplah! cowabunga!hey!';
string[0]; // => 'h'
string[string.length - 1]; // => '!'
string[3]; // => 'p'
// explode strings (convert into an array of characters)
string.split('!'); // => ['hoolpah', ' cowabunga', 'hey', '']
string(split('! ')); // => ["hooplah", "cowabunga", "hey!"]
// now you can use array methods!
// exercise example: traverse a string, add up any numbers found, and return the sum
/*
examples: use an array or object?
1) sort a list of numbers from smallest to largest.
2) which word has the greatest number of repeated letters.
3) print the number of 'a's, 'r's, and 'x's in a word.
4)
*/