filter
filter也是一个常用的操作,它用于把Array的某些元素过滤掉,然后返回剩下的元素。
和map()类似,Array的filter()也接收一个函数。和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是true还是false决定保留还是丢弃该元素。
例如,在一个Array中,删掉偶数,只保留奇数,可以这么写:
1 var arr = [1, 2, 4, 5, 6, 9, 10, 15];
2 var r = arr.filter(function (x) {
3 return x % 2 !== 0;
4 });
5 r; // [1, 5, 9, 15]
把一个Array中的空字符串删掉,可以这么写:
1 var arr = ['A', '', 'B', null, undefined, 'C', ' '];
2 var r = arr.filter(function (s) {
3 return s && s.trim(); // 注意:IE9以下的版本没有trim()方法
4 });
5 r; // ['A', 'B', 'C']
可见用filter()这个高阶函数,关键在于正确实现一个“筛选”函数。
回调函数
filter()接收的回调函数,其实可以有多个参数。通常我们仅使用第一个参数,表示Array的某个元素。回调函数还可以接收另外两个参数,表示元素的位置和数组本身:
1 var arr = ['A', 'B', 'C'];
2 var r = arr.filter(function (element, index, self) {
3 console.log(element); // 依次打印'A', 'B', 'C'
4 console.log(index); // 依次打印0, 1, 2
5 console.log(self); // self就是变量arr
6 return true;
7 });
利用filter,可以巧妙地去除Array的重复元素:
1 'use strict';
2
3 var
4 r,
5 arr = ['apple', 'strawberry', 'banana', 'pear', 'apple', 'orange', 'orange', 'strawberry'];
6 r = arr.filter(function (element, index, self) {
7 return self.indexOf(element) === index;
8 });
9 console.log(r.toString());
运行结果:
apple,strawberry,banana,pear,orange
去除重复元素依靠的是indexOf总是返回第一个元素的位置,后续的重复元素位置与indexOf返回的位置不相等,因此被filter滤掉了。
尝试用filter()筛选出素数:
1 'use strict';
2
3 function get_primes(arr) {
4 var i;
5 return arr.filter(function (element) {
6 var flag=true;
7 if(element<2){
8 flag=false;
9 }else {
10 for(var i=2;i<element;i++){
11 if (element%i==0){
12 flag=false;
13 break;
14 }
15 }
16 }
17 return flag;
18 });
19 }
20
21 // 测试:
22 var
23 x,
24 r,
25 arr = [];
26 for (x = 1; x < 100; x++) {
27 arr.push(x);
28 }
29 r = get_primes(arr);
30 if (r.toString() === [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97].toString()) {
31 console.log('测试通过!');
32 } else {
33 console.log('测试失败: ' + r.toString());
34 }
运行结果:
测试通过!
References:
Top oral steroids
References:
https://brewwiki.win/wiki/Post:Compounded_Testosterone_Enanthate_Injection
References:
Casino lisboa macau
References:
https://platform.joinus4health.eu/forums/users/babyiris93/
References:
Dbol steroids
References:
https://www.sperbys-musikplantage.de/Startseite/index.php/;focus=STRATP_com_cm4all_wdn_Flatpress_21123190&path=?x=entry:entry220830-123557%3Bcomments:1
References:
How to buy steroid online
References:
https://git.malls.iformall.com/tammieosby8243
References:
Anabolic steroids benefits
References:
https://git.hubhoo.com/mariahenning95
References:
Natural prednisone alternatives
References:
http://47.100.212.83:3000/enriquetabury
steroid supplements
References:
zumpadpro.zum.de
best injectable steroids for mass
References:
kay-lee.thoughtlanes.net
References:
San felipe casino
References:
https://squareblogs.net/jamexpert3/beste-paypal-online-casinos-casinos-mit-paypal-einzahlung-2026
References:
Rome casino
References:
https://lit-book.ru/user/dryvessel24/
References:
Rocketplay real money gaming
References:
https://graph.org/Casino-Online-Australia—Top-Platforms-Review-04-20
References:
Sky vegas casino
References:
https://no-deposit-bonus-codes-royal-ace-casino.online-spielhallen.de/
References:
Kiel
References:
https://casino-gur.online-spielhallen.de/
References:
Lübeck
References:
https://casino-royal-film.online-spielhallen.de/
References:
Mannheim
References:
https://crocoslots-casino-no-deposit-bonus.online-spielhallen.de/
References:
Cologne (Köln)
References:
https://del-oro-casino.online-spielhallen.de/
References:
Mega casino
References:
https://graph.org/Slot-Spin-Casino-04-27
References:
Genting casino reading
References:
https://graph.org/What-Is-The-Most-Successful-Roulette-Strategy-04-27
References:
Casino sider med høj udbetaling
References:
https://smallbusinessinternships.com/employer/spillemaskiner-med-den-bedste-udbetaling-i-danmark/
References:
Casinoer med høj udbetaling uden om ROFUS
References:
https://www.blurb.com/user/mirrortanker
References:
Bedste udbyttende casinoer Danmark
References:
https://portermetrics.com/en/porter_metrics_-_linkedin_pages_-_google_data_studio_report_template-1/
References:
Top online casinos
References:
https://tokemonkey.com/read-blog/7215_best-payid-casinos-australia-2026-enjoy-fast-withdrawals.html
References:
Monopoly slot machine https://tunelifystream.com