JAVASCRIPT   16

filter.js

Guest on 28th May 2021 03:33:23 AM

  1. var makeIterator = require('../function/makeIterator_');
  2.  
  3.     /**
  4.      * Array filter
  5.      */
  6.     function filter(arr, callback, thisObj) {
  7.         callback = makeIterator(callback, thisObj);
  8.         var results = [];
  9.         if (arr == null) {
  10.             return results;
  11.         }
  12.  
  13.         var i = -1, len = arr.length, value;
  14.         while (++i < len) {
  15.             value = arr[i];
  16.             if (callback(value, i, arr)) {
  17.                 results.push(value);
  18.             }
  19.         }
  20.  
  21.         return results;
  22.     }
  23.  
  24.     module.exports = filter;

Raw Paste


Login or Register to edit or fork this paste. It's free.