forked from wesbos/beginner-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays-FINISHED.html
93 lines (78 loc) · 2.43 KB
/
arrays-FINISHED.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>
</title>
<link rel="stylesheet" href="../base.css">
</head>
<body>
<script>
const names = ['wes', 'kait', 'snickers'];
console.log(names[0]);
console.log(names.length);
console.log(names[names.length - 1]);
names.push('lux');
console.log(names);
const names2 = [...names, 'lux'];
names.unshift('poppy');
console.log(names);
const names3 = ['poppy', ...names];
console.log(names3);
const bikes = ['bianchi', 'miele', 'panasonic', 'miyata'];
const newBikes = [
...bikes.slice(0, 2),
'benotto',
...bikes.slice(2)
];
const newBikes2 = [
...newBikes.slice(0, 3),
...newBikes.slice(4)
];
console.log(newBikes2);
//
const comments = [
{ text: 'Cool Beans', id: 123 },
{ text: 'Love this', id: 133 },
{ text: 'Neato', id: 233 },
{ text: 'good bikes', id: 333 },
{ text: 'so good', id: 433 },
];
function deleteComment(id, comments) {
// first find the index of the item in the array
const commentIndex = comments.findIndex(comment => comment.id === id);
// make a new array without that item in it
return [
...comments.slice(0, commentIndex),
...comments.slice(commentIndex + 1),
];
// return our new array
}
const kaitIndex = names.findIndex(name => name === 'kait');
const newNamesWithOutKait = [
// get everything up to kait index
...names.slice(0, kaitIndex),
// everything after Kait index
...names.slice(kaitIndex + 1)
];
console.log(newNamesWithOutKait);
// const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// console.log(numbers);
// numbers.splice(3, 2);
// console.log(numbers);
// // Mutation Methods - DO CHANGE THE ORIGINAL DATA
// const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// // anytime you want to use a mutation method and NOT mutate the orignal array
// // we need to take a copy of the array
// const numbersReversed = [...numbers].reverse();
// console.log(numbersReversed);
// // numbers.reverse();
// console.log(numbers);
// // Immutable - THEY DO NOT CHANGE THE ORIGINAL DATA
// const pizzaSlice = numbers.slice(2, 5);
// console.log(pizzaSlice);
// console.log(numbers);
</script>
</body>
</html>