zoukankan      html  css  js  c++  java
  • Array.prototype.filter()

      1 <!DOCTYPE html>
      2 <html>
      3 
      4     <head>
      5         <meta charset="UTF-8">
      6         <title></title>
      7     </head>
      8 
      9     <body>
     10         <script type="text/javascript">
     11             var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
     12 
     13             const result = words.filter(word => word.length > 6);
     14 
     15             console.log(result);
     16 
     17             function isBigEnough(value) {
     18                 if(value >= 10){
     19                     return true;//=== return value >=10
     20                 }
     21             }
     22 
     23             var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
     24             console.log(filtered)
     25             
     26             
     27             
     28             
     29             
     30             
     31             
     32             var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
     33 
     34 /**
     35  * Array filters items based on search criteria (query)
     36  */
     37 function filterItems(query) {
     38     //基于搜索的字母来从数组中找类似的选项
     39   return fruits.filter(function(el) {
     40       return el.toLowerCase().indexOf(query.toLowerCase()) > -1;
     41   })
     42 }
     43 
     44 console.log(filterItems('ap')); // ['apple', 'grapes']
     45 console.log(filterItems('an')); // ['banana', 'mango', 'orange']
     46 
     47 
     48 const fruits1 = ['apple', 'banana', 'grapes', 'mango', 'orange'];
     49 
     50 /**
     51  * Array filters items based on search criteria (query)
     52  */
     53 const filterItems1 = (query) => {
     54     //箭头函数的写法
     55   return fruits1.filter(el => el.toLowerCase().indexOf(query.toLowerCase()) > -1);
     56 };
     57 
     58 console.log(filterItems1('ap')); // ['apple', 'grapes']
     59 console.log(filterItems1('an')); // ['banana', 'mango', 'orange']
     60 
     61 
     62 
     63 
     64 
     65 
     66 
     67 if (!Array.prototype.filter){
     68   Array.prototype.filter = function(func, thisArg) {
     69     'use strict';
     70     if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
     71         throw new TypeError();
     72    
     73     var len = this.length >>> 0,//因爲雖然這個 map 方法是在Array 的 prototype上的,
     74     //但實際上調用的時候,this 不一定是 Array類型,length不能得到保證,加上位運算後,可以將不確定的値轉換成Number。
     75         res = new Array(len), // preallocate array
     76         t = this, c = 0, i = -1;
     77     if (thisArg === undefined){
     78       while (++i !== len){
     79         // 检查是否设置了密钥
     80         if (i in this){
     81           if (func(t[i], i, t)){
     82             res[c++] = t[i];
     83           }
     84         }
     85       }
     86     }
     87     else{
     88       while (++i !== len){
     89         // 检查是否设置了密钥
     90         if (i in this){
     91           if (func.call(thisArg, t[i], i, t)){
     92             res[c++] = t[i];
     93           }
     94         }
     95       }
     96     }
     97    
     98     res.length = c; // 将数组缩小到适当的大小
     99     return res;
    100   };
    101 }
    102         </script>
    103     </body>
    104 
    105 </html>

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter  资料来自MDN

  • 相关阅读:
    ubuntu 12.04 配置双屏幕教程
    window打开服务的dos命令
    ffmpeg 视频转换和截图 方法
    pycharm
    Django mptt介绍以及使用
    浅谈Python Web框架
    activity 的属性android:taskAffinity和android:allowTaskReparenting
    android TranslateAnimation动画详解
    Android中Activity启动模式详解
    android:descendantFocusability用法简析
  • 原文地址:https://www.cnblogs.com/lujunan/p/10342960.html
Copyright © 2011-2022 走看看