-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayMethods.js
58 lines (45 loc) · 1.43 KB
/
arrayMethods.js
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
const patients = require("./patients");
const names = ["Matias", "Swen", "David", "Sebastian", "Gonzalo"];
const longerThan6 = names.find((name) => {
return name.length > 6;
});
console.log("longerThan6", longerThan6);
// {
// id: 2401,
// firstName: "Cynde",
// lastName: "Dohmann",
// phoneNumber: "+98-468-937-0605",
// email: "[email protected]",
// gender: "f",
// height: 1.76,
// weight: 97,
// dailyExercise: "no",
// age: 49,
// },
// .find
// returns the first element that is true for the given condition
// Returns? => element || undefined
const someId = 2444;
const byId = patients.find((p) => {
return p.id === someId;
});
const searchEmail = "[email protected]";
const byEmail = patients.find((p) => {
const isAMatch = p.email === searchEmail; // true || false
return isAMatch;
});
console.log("found the patient?", byEmail);
// FILTER
// returns all the elements that are true for the given condition
// RETURNS => A new array, always. Might have elements or it might be empty
const femalePatients = patients.filter((p) => {
return p.gender === "f";
});
// console.log("female Patients", femalePatients);
console.log("original patients", patients.length);
console.log("amount of female patients", femalePatients.length);
// list of patients that are under 35 AND don't exercise
const result = patients.filter((p) => {
return p.age < 45 && p.dailyExercise === "no";
});
console.log("result", result);