-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
116 lines (90 loc) · 3.27 KB
/
example.js
File metadata and controls
116 lines (90 loc) · 3.27 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
console.log("this is a method")
// NOTE a method is just a function that is attached to an object (or object like)
let objectWithMethod = {
thisMethod: () => {
let a = 10
let b = 5
return a + b
}
}
// Common Array methods
let cats = [
{ name: 'Falcon', picture: '😼', likes: '🐟' },
{ name: 'Bean', picture: '😺', likes: '🧀' },
{ name: 'Abomination', picture: '🙀', likes: '💣' },
{ name: 'Ratatouille', picture: '🙀', likes: '🍝' },
]
function introduceCats() {
for (let i = 0; i < cats.length; i += 1) {
console.log(`
This is ${cats[i].name}${cats[i].picture}, they like to eat ${cats[i].likes}.
`)
}
// .forEach
// forEach acts like a for loop, running a bit of code "for each" item in an array. It also gives access to that item through a "parameter" (call it what you want, banana is a bad name, just an example)
cats.forEach((banana) => {
console.log('🐈', `This is ${banana.name}${banana.picture}, they like to eat ${banana.likes}.`)
})
}
// also a function technically
// let doThis = () => { console.log('some code') }
// .find
// finds the {name: 'Bean',...} and give us a reference to that object called capturedBean
let capturedBean = cats.find((cat) => cat.name == 'Bean')
// finds the *first* {name: ?,..} that isn't 'Dr. Strange' and gives us reference to it, in this case it would be {name: 'Falcon',...}
let capturedCat = cats.find((cat) => cat.name !== 'Dr. Strange')
// find doesn't find an {} so instead it returns *undefined*
let notFoundCat = cats.find((cat) => cat.name == 'Red Hulk')
// .filter
// similar to find, but returns and array of ALL matching items, instead of the first
let catsThatAreNotDrStrange = cats.filter((cat) => cat.name !== 'Dr. Strange')
let catsThatAreNotFalcon = cats.filter((cat) => cat.name !== 'Falcon')
let frightenedCats = cats.filter((cat) => cat.picture == '🙀')
// .findLast
let lastNotRedHulkCat = cats.findLast((cat) => cat.name !== 'Red Hulk')
// .findIndex
let positionOfBean = cats.findIndex((cat) => cat.name == 'Bean')
// .findLastIndex
let lastPositionOfScaredCat = cats.findLastIndex((cat) => cat.picture == '🙀')
// .indexOf (if you have a pointer to an object, or a primitive type, this works like findIndex, without the method)
let indexOfBean = cats.indexOf(capturedBean)
// .map
// Creates a new array, with transformed information in it from the function
let numbers = [1, 5, 10, -100]
let items = [
{ pic: '🧀', price: 1.11, qty: 2 },
{ pic: '🍕', price: 3, qty: 0 },
{ pic: '🍝', price: 5, qty: 1 },
]
let catNames = cats.map((cat) => cat.name)
let catShoppingList = cats.map((cat) => cat.likes)
// ['🐟','🧀','💣','🍝']
let doubledNums = numbers.map((number) => number * 2)
// [ 2, 10, 20, -200]
let sumsOfItems = items.map((item) => item.price * item.qty)
// [ 2.22, 0, 5]
let dogs = cats.map((cat) => { return { name: cat.name, picture: '🐶', likes: '🦴' } })
/**[
{
"name": "Falcon",
"picture": "🐶",
"likes": "🦴"
},
{
"name": "Bean",
"picture": "🐶",
"likes": "🦴"
},
{
"name": "Abomination",
"picture": "🐶",
"likes": "🦴"
},
{
"name": "Ratatouille",
"picture": "🐶",
"likes": "🦴"
}
]
*
*/