es6 filter() 數(shù)組過濾方法總結(jié)
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"){
// 如果條件滿足就把當前的值推入
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- es6數(shù)組的flat(),flatMap()函數(shù)用法實例分析
- 淺談ES6新增的數(shù)組方法和對象
- ES6新特征數(shù)字、數(shù)組、字符串
- ES6數(shù)組的擴展詳解
- ES6中數(shù)組array新增方法實例總結(jié)
- ES6使用Set數(shù)據(jù)結(jié)構(gòu)實現(xiàn)數(shù)組的交集、并集、差集功能示例
- ES6 新增的創(chuàng)建數(shù)組的方法(小結(jié))
- ES6新特性七:數(shù)組的擴充詳解
- ES6中的數(shù)組擴展方法
- ES6基礎(chǔ)之數(shù)組和對象的拓展實例詳解
- ES6學習筆記之字符串、數(shù)組、對象、函數(shù)新增知識點實例分析
- es6數(shù)組includes()用法實例分析
相關(guān)文章
bootstrap-table組合表頭的實現(xiàn)方法
本篇文章主要介紹了bootstrap-table組合表頭的實現(xiàn)方法,非常具有實用價值,需要的朋友可以參考下2017-09-09
微信小程序 如何獲取網(wǎng)絡(luò)狀態(tài)
這篇文章主要介紹了微信小程序 如何獲取網(wǎng)絡(luò)狀態(tài),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07
JavaScript人臉識別技術(shù)及臉部識別JavaScript類庫Tracking.js
人臉識別的JavaScript程序包是Face Detection,它是由Jay Salvat和Liu Liu開發(fā)的。它是一個標準的jQuery插件,通過對提供的圖片進行分析,返回所有找到的臉部圖像的坐標,感興趣的朋友跟著小編一起學習js人臉識別技術(shù)及臉部識別JavaScript類庫Tracking.js吧2015-09-09
JSON在Javascript中的使用(eval和JSON.parse的區(qū)別)詳細解析
這篇文章主要介紹了JSON在Javascript中的使用以及eval和JSON.parse的區(qū)別詳細解析,本文對eval()和JSON.parse()的區(qū)別介紹的非常詳細,需要的朋友可以參考下2017-09-09

