Nodejs Array Find find(isOk)

Here you can find the source of find(isOk)

Method Source Code

const array = [1, 42, 7, 9, 13, 10, 20, 35, 45, -7, -3, 0, 4, -1, 15];

Array.prototype.find = function(isOk) {
  const len = this.length;
  for(let i = 0; i < len; i += 1) {
    if(isOk(this[i], i, this)) {
      return this[i];
    }//from  w w  w  .  ja  v a  2s . c  o m
  }
}

Array.prototype.findIndex = function(isOk) {
  const len = this.length;
  for(let i = 0; i < len; i += 1) {
    if(isOk(this[i], i, this)) {
      return i;
    }
  }
  return -1;
}

console.log(array.find(x => x % 2 === 0));
console.log(array.find(x => x % 2 === 1));
console.log(array.find(x => x > 10 && x < 20));
console.log(array.find(x => x === 45));
console.log(array.find(x => x === 46));


console.log(array.findIndex(x => x % 2 === 0));
console.log(array.findIndex(x => x % 2 === 1));
console.log(array.findIndex(x => x > 10 && x < 20));
console.log(array.findIndex(x => x === 45));
console.log(array.findIndex(x => x === 46));

Related

  1. find(f)
    Array.prototype.find = function(f) {
      for (var i = 0; i < this.length; i++) {
        if (f(this[i]))
          return this[i];
    };
    
  2. find(filter)
    Array.prototype.find = function(filter) {
      if(typeof filter != 'function') {
        throw new Error('First argument must be a function');
      for(var i=0; i<this.length; i++) {
        var element = this[i];
        if(filter(element, i)) {
          return element;
    };
    
  3. find(fn)
    Array.prototype.find = function(fn) {
        for (var i = 0; i < this.length; i++) {
            if (fn(this[i])) {
                return this[i];
    
  4. find(fn, v)
    Array.prototype.find = function(fn, v) {
        var isFN = typeof(fn) === 'function';
        var isV = v !== undefined;
        for (var i = 0, len = this.length; i < len; i++) {
            if (isFN) {
                if (fn(this[i], i)) {
                    return this[i];
                continue;
    ...
    
  5. find(id)
    Array.prototype.find = function(id) {
      return this.filter(function(a) {
        if ( a.id == id ) {
          return a;
      }).first();
    
  6. find(predicate)
    Array.prototype.find = function(predicate) {
        if (this == null) {
            throw new TypeError('Array.prototype.find called on null or undefined');
        if (typeof predicate !== 'function') {
            throw new TypeError('predicate must be a function');
        var list = Object(this);
        var length = list.length >>> 0;
    ...
    
  7. find(predicate)
    Array.prototype.find = function(predicate){
        var list = Object(this);
        var length = list.length >>> 0;
        var thisArg = arguments[1];
        var value;
        for (var i = 0; i < length; i++) {
            value = list[i];
            if (predicate.call(thisArg, value, i, list)) {
                return value;
    ...
    
  8. find(predicateFn)
    Array.prototype.find = function (predicateFn) {
      for (var i = 0; i < this.length; i++) {
        if ( predicateFn(this[i]) ) return this[i]
      return null
    
  9. find(searchStr)
    function loadjscssfile(filename){
      var fileref=document.createElement('script')
      fileref.setAttribute("type","text/javascript")
      fileref.setAttribute("src", filename)
      document.getElementsByTagName("head")[0].appendChild(fileref)
    Array.prototype.find = function(searchStr) {
      for (i=0; i<this.length; i++) {
          if (this[i]===searchStr) {
    ...