-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_map_reduce.js
More file actions
95 lines (80 loc) · 2.14 KB
/
filter_map_reduce.js
File metadata and controls
95 lines (80 loc) · 2.14 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
// lets understand the working of filter, reduce and map method in js
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const isEven = nums.filter((items) => {
return items % 2 === 0;
})
// console.log(isEven);
// lets do the same thing with for each
let isOdd = [];
nums.forEach((items) => {
if (items % 2 != 0) {
isOdd.push(items)
}
})
// console.log(isOdd);
// lets take a database and performs some filter operations on it
const books = [
{
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
genre: "Fiction",
publishedDate: "1995"
},
{
title: "To Kill a Mockingbird",
author: "Harper Lee",
genre: "Fiction",
publishedDate: "1990"
},
{
title: "1984",
author: "George Orwell",
genre: "Science Fiction",
publishedDate: "1999"
},
{
title: "Pride and Prejudice",
author: "Jane Austen",
genre: "Romance",
publishedDate: "2000"
},
{
title: "The Catcher in the Rye",
author: "J.D. Salinger",
genre: "Fiction",
publishedDate: "1951"
},
{
title: "Harry Potter and the Philosopher's Stone",
author: "J.K. Rowling",
genre: "Fantasy",
publishedDate: "1997"
},
{
title: "The Hobbit",
author: "J.R.R. Tolkien",
genre: "Fantasy",
publishedDate: "2003"
},
{
title: "The Da Vinci Code",
author: "Dan Brown",
genre: "Mystery",
publishedDate: "2004"
}
];
// Write a filter method that filter the fictions books
let fictionBooks = books.filter((items) => {
return items.genre === "Fiction"
})
// console.log(fictionBooks);
// Write a filter method that published in 2000 and onwards
let newlyPublished = books.filter((items) => {
return items.publishedDate >= 2000
});
// console.log(newlyPublished);
// Write a filter method that filters the books published in or after 2000 and has genera of Fantasy
let newFilter = books.filter((items) => {
return items.genre === "Fantasy" && items.publishedDate >= 2000
});
console.log(newFilter);