/** 场景1
*
* 假定有一个对象数组(A),获取数组中指定类型的对象放到B数组中
*/
var porducts = [
{name:"cucumber",type:"vegetable"},
{name:"banana",type:"fruit"},
{name:"celery",type:"vegetable"},
{name:"orange",type:"fruit"}
];
// es5
var filteredProducts = [];
for(var i = 0; i < porducts.length; i++){
if(porducts[i].type === "fruit"){
filteredProducts.push(porducts[i]);
}
}
// console.log(filteredProducts);
// es6 filter
var filtered2 = porducts.filter(function(product){
return product.type === "vegetable";
})
// console.log(filtered2);
/** 场景2
*
* 假定有一个对象数组(A),过滤掉不满足以下条件的对象
* 条件: 蔬菜 数量大于0,价格小于10
*/
var products = [
{name:"cucumber",type:"vegetable",quantity:0,price:1},
{name:"banana",type:"fruit",quantity:10,price:16},
{name:"celery",type:"vegetable",quantity:30,price:8},
{name:"orange",type:"fruit",quantity:3,price:6}
];
products = products.filter(function(product){
return product.type === "vegetable"
&& product.quantity > 0
&& product.price < 10
})
console.log(products);
/** 场景3
*
* 假定有两个数组(A,B),根据A中id值,过滤掉B数组不符合的数据
*/
var post = {id:4,title:"Javascript"};
var comments = [
{postId:4,content:"Angular4"},
{postId:2,content:"Vue.js"},
{postId:3,content:"Node.js"},
{postId:4,content:"React.js"},
];
function commentsForPost(post,comments){
return comments.filter(function(comment){
return comment.postId === post.id;
})
}
console.log(commentsForPost(post,comments));
3.filter
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 系列故事的第3篇介绍RxJava的过滤API debounce debounce方法会过滤掉发射速率过快的数据项,...
- 这个页面展示的操作符可用于过滤和选择Observable发射的数据序列。Debounce — 仅在过了一段指定的时...