Array.every(x=>x)是每一個(gè)都要滿足
Array.some(x=>x)是有一個(gè)滿足豪娜。
Array.find(findIndex)绘盟,返回符合條件的第一個(gè)值表伦。
Array.filter(過濾成新的數(shù)組)
數(shù)組的方法分為兩類
1)改變?cè)瓟?shù)組
push,pop,shift,unshift,sort,reverse,splice
2)不改變?cè)瓟?shù)組concat,join-->
split迄沫,toStringpush:從數(shù)組最后一位開始加數(shù)據(jù)
pop:把數(shù)組最后一位剪切
shift:在數(shù)組最前一位剪切
unshift:在數(shù)組最前一位加數(shù)
reverse:把原數(shù)組逆轉(zhuǎn)
splice:arr.splice(從第幾位開始体啰,截取多少長(zhǎng)度攒巍,在切口處添加新數(shù)據(jù))
concat :連接join:返回字符串
slice:截取arr.slice(從該為開始截取,截取到該為)
1.創(chuàng)建一個(gè)數(shù)組荒勇,判斷數(shù)組中是否存在某個(gè)值
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ù)組中不符合項(xiàng)
var arr = [20,30,50, 96,50]
var newArr = arr.filter(item => item>40)
console.log(newArr)
5.過濾不符合項(xiàng)
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]
/*
有一個(gè)對(duì)象數(shù)組 a ,將a數(shù)中對(duì)象某個(gè)屬性的值存儲(chǔ)到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){//對(duì)porducts數(shù)組對(duì)象過濾如果porduct.type === "fruit"就return出去,再用一個(gè)變量接住
return porduct.type === "fruit"
})
console.log(filter2)
/*
需求二
有一個(gè)對(duì)象數(shù)組A,過濾掉不滿足以下條件對(duì)象
條件:蔬菜 數(shù)量大于0 價(jià)格小于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)
/*
需求三:
有兩個(gè)數(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)