-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_filter_reduce.js
54 lines (44 loc) · 1.3 KB
/
map_filter_reduce.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
const users = [
{
id: 1,
name: 'ryan',
email: '[email protected]',
languages: ['clojure', 'javascript'],
},
{
id: 2,
name: 'luis',
email: '[email protected]',
languages: ['java', 'scala', 'php'],
},
{
id: 3,
name: 'zach',
email: '[email protected]',
languages: ['javascript', 'bash'],
},
{
id: 4,
name: 'fernando',
email: '[email protected]',
languages: ['java', 'php', 'sql'],
},
{
id: 5,
name: 'justin',
email: '[email protected]',
languages: ['html', 'css', 'javascript', 'php'],
},
];
// using .filter to create an array where each user has at least 3 languages in the languages array
let moreThanThree = users.filter(user => user.languages.length >= 3);
console.log(moreThanThree);
// using .map to create an array of strings where each element is a user's email address
let emailAddress = users.map(user => user.email);
console.log(emailAddress);
// using .reduce to transform the array into an object where the object's keys are ids and the values are objects that represent each user
let reducedUsers = users.reduce((newObject, user) => {
newObject[user.id] = user;
return newObject;
}, {});
console.log(reducedUsers);