generated from Code-Institute-Org/ci-advanced-js-filter-method
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
86 lines (76 loc) · 1.84 KB
/
filter.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
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
/**
* To run this file in Gitpod, use the
* command node filter.js in the terminal
*/
// Simple Filtering
const people = [
{
name: 'Michael',
age: 23,
},
{
name: 'Lianna',
age: 16,
},
{
name: 'Paul',
age: 18,
},
];
const oldEnough = people.filter(people => people.age >= 21);
console.log(oldEnough);
const paul = people.filter(p => p.name == 'Paul');
console.log(paul);
const paulObj = people.filter(p => p.name == 'Paul')[0];
console.log(paulObj);
// Complex Filtering
const students = [
{
id: 1,
name: 'Mark',
profession: 'Developer',
skills: [
{ name: 'javascript', yrsExperience: 1 },
{ name: 'html', yrsExperience: 5 },
{ name: 'css', yrsExperience: 3 },
],
},
{
id: 2,
name: 'Ariel',
profession: 'Developer',
skills: [
{ name: 'javascript', yrsExperience: 0 },
{ name: 'html', yrsExperience: 4 },
{ name: 'css', yrsExperience: 2 },
],
},
{
id: 3,
name: 'Jason',
profession: 'Designer',
skills: [
{ name: 'javascript', yrsExperience: 1 },
{ name: 'html', yrsExperience: 1 },
{ name: 'css', yrsExperience: 5 },
],
},
];
// more difficult to read
// const candidates = students.filter(student => {
// const strongSkills = student.skills.filter(
// skills => skills.yrsExperience >= 5
// );
// return strongSkills.length > 0;
// });
// a more easy to read and elegant approach
const has5YrsExp = skills => skills.yrsExperience >= 5;
const hasStrongSkills = student => {
const strongSkills = student.skills.filter(has5YrsExp);
return strongSkills.length > 0;
};
const candidates = students.filter(hasStrongSkills);
console.log(candidates);
// extract and print only candidates names
const candidatesNames = candidates.map(candidate => candidate.name);
console.log('Candidates:', candidatesNames);