Array.every(x=>x)是每一個都要滿足
Array.some(x=>x)是有一個滿足。
Array.find(findIndex),返回符合條件的第一個值。
Array.filter(過濾成新的數組)
數組的方法分為兩類
1)改變原數組
push,pop,shift,unshift,sort,reverse,splice
2)不改變原數組concat,join-->
split,toStringpush:從數組最后一位開始加數據
pop:把數組最后一位剪切
shift:在數組最前一位剪切
unshift:在數組最前一位加數
reverse:把原數組逆轉
splice:arr.splice(從第幾位開始,截取多少長度,在切口處添加新數據)
concat :連接join:返回字符串
slice:截取arr.slice(從該為開始截取,截取到該為)
示例
1.創建一個數組,判斷數組中是否存在某個值
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.去掉空數組空字符串、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.去掉數組中不符合項
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.數組去重
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
/* 有一個對象數組 a ,將a數中對象某個屬性的值存儲到B數組中*/ var porducts = [ {name:"cucumber",type:"vegetable"}, {name:"banana",type:"fruit"}, {name:"celery",type:"vegetable"}, {name:"orange",type:"fruit"},];// es5var filteredProducts = [];for(var i = 0;i < porducts.length; i ++){ if(porducts[i].type === "fruit"){ // 如果條件滿足就把當前的值推入 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數組對象過濾如果porduct.type === "fruit"就return出去,再用一個變量接住 return porduct.type === "fruit"})console.log(filter2)
8
/* 需求二 有一個對象數組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)//0: {name: "celery", type: "vegetable", quantity: 30, price: 8}name: "celery"price: 8quantity: 30type: "vegetable"__proto__: Objectlength: 1__proto__: Array(0)
9
/* 需求三: 有兩個數組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))// 0: {postId: 4, content: "Angular4"}1: {postId: 4, content: "React.js"}length: 2__proto__: Array(0)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答