欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

es6 filter() 數(shù)組過濾方法總結(jié)

 更新時間:2019年04月03日 11:32:46   作者:yang  
這篇文章主要介紹了es6 filter() 數(shù)組過濾方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Array.every(x=>x)是每一個都要滿足

Array.some(x=>x)是有一個滿足。

Array.find(findIndex),返回符合條件的第一個值。

Array.filter(過濾成新的數(shù)組)

數(shù)組的方法分為兩類

1)改變原數(shù)組

push,pop,shift,unshift,sort,reverse,splice

2)不改變原數(shù)組concat,join-->

split,toStringpush:從數(shù)組最后一位開始加數(shù)據(jù)

pop:把數(shù)組最后一位剪切

shift:在數(shù)組最前一位剪切

unshift:在數(shù)組最前一位加數(shù)

reverse:把原數(shù)組逆轉(zhuǎn)

splice:arr.splice(從第幾位開始,截取多少長度,在切口處添加新數(shù)據(jù))

concat :連接join:返回字符串

slice:截取arr.slice(從該為開始截取,截取到該為)

示例

1.創(chuàng)建一個數(shù)組,判斷數(shù)組中是否存在某個值

var newarr = [
 { num: 1, val: 'ceshi', flag: 'aa' },
 { num: 2, val: 'ceshi2', flag: 'aa2' }
]
console.log(newarr.filter(item => item.num===2 ))

2.也可以通過上面方法過濾掉num為2的留下num為1的

var newarr = [
 { num: 1, val: 'ceshi', flag: 'aa' },
 { num: 2, val: 'ceshi2', flag: 'aa2' }
]
console.log(newarr.filter(item => item.num!=2 ))

3.去掉空數(shù)組空字符串、undefined、null

var arr = ['1','2',undefined, '3.jpg',undefined]
var newArr = arr.filter(item => item)
console.log(newArr)

var arr = ['1','2',null, '3.jpg',null]
var newArr = arr.filter(item => item)
console.log(newArr)

>//空字符串里面不能包含空格
var arr = ['1','2','', '3.jpg','']
var newArr = arr.filter(item => item)
console.log(newArr)

4.去掉數(shù)組中不符合項

var arr = [20,30,50, 96,50]
var newArr = arr.filter(item => item>40) 
console.log(newArr)

5.過濾不符合項

var arr = ['10','12','23','44','42']
var newArr = arr.filter(item => item.indexOf('2')<0) 
console.log(newArr)

6.數(shù)組去重

var arr = [1, 2, 2, 3, 4, 5, 5, 6, 7, 7,8,8,0,8,6,3,4,56,2];
var arr2 = arr.filter((x, index,self)=>self.indexOf(x)===index) 
console.log(arr2); //[1, 2, 3, 4, 5, 6, 7, 8, 0, 56]

7

/*
  有一個對象數(shù)組 a ,將a數(shù)中對象某個屬性的值存儲到B數(shù)組中
*/ 
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"){
    // 如果條件滿足就把當(dāng)前的值推入
    filteredProducts.push(porducts[i])
  }
}
// console.log(filteredProducts)//0: {name: "banana", type: "fruit"}1: {name: "orange", type: "fruit"}length: 2__proto__: Array(0)
// ES6
 var filter2 = porducts.filter(function(porduct){//對porducts數(shù)組對象過濾如果porduct.type === "fruit"就return出去,再用一個變量接住
  return porduct.type === "fruit"
})
console.log(filter2)

8

/*
  需求二
  有一個對象數(shù)組A,過濾掉不滿足以下條件對象
  條件:蔬菜 數(shù)量大于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)//0: {name: "celery", type: "vegetable", quantity: 30, price: 8}name: "celery"price: 8quantity: 30type: "vegetable"__proto__: Objectlength: 1__proto__: Array(0)

9

/*
  需求三:
  有兩個數(shù)組A,B,根據(jù)A中的ID值 ,過濾掉B數(shù)組不符合的數(shù)據(jù)
*/ 
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))
// 0: {postId: 4, content: "Angular4"}1: {postId: 4, content: "React.js"}length: 2__proto__: Array(0)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論