Javascript Array filter(fun)

Description

Javascript Array filter(fun)


"use strict";/*from   www .jav  a 2  s .c  o  m*/

Array.prototype.filter = function (fun) {
    let arr = [];
    let thisArg = arguments.length >= 2 ? arguments[1] : void 0;

    for (let i = 0; i < this.length; i++) {
        if (fun.call(thisArg, this[i], i, this)) {
            arr.push(this[i]);
        }
    }
    return arr;
}

Javascript Array filter(fun)

"use strict";/* www  . j  a  v a2  s  .c  om*/

Array.prototype.filter = function (fun) {
    let filtered = [];

    for (let i = 0; i < this.length; i++) {
        if (fun(this[i])) {
            filtered.push(this[i]);
        }
    }
    return filtered;
};

console.log([1, 2, 3, 4].filter((num)=> {
    return num > 3
}));

Javascript Array filter(fun)

// Implement a Filter function
// https://www.codewars.com/kata/implement-a-filter-function

Array.prototype.filter = function (fun) {
    var len = this.length;
    var res = [];
    var thisArg = void 0;
    for (var i = 0; i < len; i++) {
        var val = this[i];
        if (fun.call(thisArg, val)) {
            res.push(val);//from w w w.ja v a  2  s. co  m
        }
    }
    return res;
};

Javascript Array filter(fun)

Array.prototype.filter = function(fun) {
  "use strict";//w  w w . j a  v a  2s.com
  var i, len, res, t, thisArg, val;
  if (this === void 0 || this === null) {
    throw new TypeError();
  }
  t = Object(this);
  len = t.length >>> 0;
  if (typeof fun !== "function") {
    throw new TypeError();
  }
  res = [];
  thisArg = (arguments_.length >= 2 ? arguments_[1] : void 0);
  i = 0;
  while (i < len) {
    if (i in t) {
      val = t[i];
      if (fun.call(thisArg, val, i, t)) {
        res.push(val);
      }
    }
    i++;
  }
  return res;
};



PreviousNext

Related