Skip to content

Commit 2c16d46

Browse files
committed
filter method
1 parent 0d3ffd7 commit 2c16d46

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

app/index.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,53 @@ var prices = cars.map(function(car) {
6464
return car.price;
6565
});
6666

67-
console.log(prices);
67+
console.log(prices);
68+
69+
70+
/** filter helper **/
71+
72+
var products = [
73+
{ name: 'cucumber', type: 'vegetable', quantity: 0, price: 1 },
74+
{ name: 'orange', type: 'fruit', quantity: 10, price: 15},
75+
{ name: 'celery', type: 'vegetable', quantity: 30, price: 9},
76+
{ name: 'banana', type: 'fruit', quantity: 3, price: 5}
77+
];
78+
79+
var filteredProducts = []
80+
81+
for (var i=0; i < products.length; i++) {
82+
if (products[i].type === 'fruit') {
83+
filteredProducts.push(products[i]);
84+
}
85+
}
86+
87+
var returnedStuff = products.filter(function(product) {
88+
return product.type === 'fruit';
89+
});
90+
91+
console.log(filteredProducts);
92+
console.log(returnedStuff);
93+
94+
//Type is vegetable quantity is greater htan 0, price is less than 10
95+
96+
var multiFilter = products.filter(function(product){
97+
return product.type === 'vegetable' && product.quantity > 0 && product.price < 10;
98+
});
99+
100+
console.log(multiFilter);
101+
102+
var post = {id: 4, title: "New Post"};
103+
var comments = [
104+
{postId: 4, content : "awesome post"},
105+
{postId: 3, content : "it was okay"},
106+
{postId: 4, content : "neat"}
107+
];
108+
109+
function commentsForPost(post, comments) {
110+
return comments.filter(function (comment) {
111+
return comment.postId === post.id;
112+
});
113+
}
114+
115+
console.log(commentsForPost(post, comments));
116+

0 commit comments

Comments
 (0)