-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.js
86 lines (67 loc) · 2.04 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
///////////////////////
// ES6 - filter helper
///////////////////////
/*
var products = [
{ name: 'cucumber', type: 'vegetable' },
{ name: 'banana', type: 'fruit' },
{ name: 'celery', type: 'vegetable' },
{ name: 'orange', type: 'fruit' }
];
var filteredProducts1 = [];
var filteredProducts2 = [];
// ES5
for (var i = 0; i < products.length; i++) {
if (products[i].type === 'fruit') {
filteredProducts1.push(products[i]);
}
}
console.log(filteredProducts1);
// ES6 - .filter
var filteredProducts2 = products.filter(function(product) {
return product.type === 'fruit';
});
console.log(filteredProducts2)
// ==========================================
var products = [
{ name: 'cucumber', type: 'vegetable', quantity: 0, price: 1 },
{ name: 'banana', type: 'fruit', qunatity: 10, price: 15 },
{ name: 'celery', type: 'vegetable', quantity: 30, price: 13 },
{ name: 'orange', type: 'fruit', quantity: 3, price: 5 }
];
var filteredProducts = products.filter(function(product) {
return product.type === 'vegetable' && product.quantity > 0 && product.price < 14;
});
console.log(filteredProducts);
// ===========================================
var post = { id:4, title: 'New Post' };
var comments = [
{ postId: 4, content: 'awesome post' },
{ postId: 3, content: 'it was ok' },
{ postId: 4, content: 'neat'}
];
function commentsForPost(post, comments) {
return comments.filter(function(comment) {
return comment.postId === post.id;
});
}
console.log(commentsForPost(post, comments));
// ===========================================
// Filtering Values
var numbers = [15, 25, 35, 45, 55, 65, 75, 85, 95];
var filteredNumbers = numbers.filter(function(number) {
return number > 50;
});
// ===========================================
// Handling Permissions with Filter
var users = [
{ id: 1, admin: true },
{ id: 2, admin: false },
{ id: 3, admin: false },
{ id: 4, admin: false },
{ id: 5, admin: true },
];
var filteredUsers = users.filter(function(user) {
return user.admin;
});
*/