Nodejs Array Include includes(searchElement /*, fromIndex*/ )

Here you can find the source of includes(searchElement /*, fromIndex*/ )

Method Source Code

// from MDN//from   ww w.j  a v a2s.  c om
if (![].includes) {
  Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
    if (this === undefined || this === null) {
      throw new TypeError('Cannot convert this value to object');
    }
    var O = Object(this);
    var len = parseInt(O.length, 10) || 0;
    if (len === 0) {
      return false;
    }
    var n = parseInt(arguments[1], 10) || 0;
    var k;
    if (n >= 0) {
      k = n;
    } else {
      k = len + n;
      if (k < 0) k = 0;
    }
    while (k < len) {
      var currentElement = O[k];
      if (searchElement === currentElement ||
         (searchElement !== searchElement && currentElement !== currentElement)) {
        return true;
      }
      k++;
    }
    return false;
  };
}

// http://stackoverflow.com/a/12042959
Array.prototype.unique = function(){
  'use strict';
  var im = {}, uniq = [];
  for (var i=0;i<this.length;i++){
    var type = (this[i]).constructor.name, 
    //          ^note: for IE use this[i].constructor!
        val = type + (!/num|str|regex|bool/i.test(type) 
               ? JSON.stringify(this[i]) 
               : this[i]);
    if (!(val in im)){uniq.push(this[i]);}
    im[val] = 1;
  }
  return uniq;
}

Related

  1. includes(element)
    Array.prototype.includes = function(element) {
      return this.indexOf(element) !== -1;
    };
    
  2. includes(item)
    Array.prototype.includes = function(item) {
        return this.indexOf(item) > -1;
    
  3. includes(matcher)
    Array.prototype.includes = function(matcher) {
      var includes = false
        , arr = this;
      for( var j = 0, str; str = arr[j++]; ) {
        if( typeof str === "string" && str.includes(matcher) ) {
          includes = true;
      return includes;
    ...
    
  4. includes(s)
    Array.prototype.includes = function(s) {
        return this.indexOf(s) > -1;
    };
    
  5. includes(searchElement /*, fromIndex*/ )
    Array.prototype.includes = Array.prototype.includes || (function(searchElement  ) {
      var O = Object(this);
      var len = parseInt(O.length, 10) || 0;
      if (len === 0) return false;
      var n = parseInt(arguments[1], 10) || 0;
      var k;
      if (n >= 0) {
        k = n;
      } else {
    ...
    
  6. includes(searchElement /*, fromIndex*/)
    if (!Array.prototype.includes) {
      Array.prototype.includes = function(searchElement ) {
        'use strict';
        if (this == null) {
          throw new TypeError('Array.prototype.includes called on null or undefined');
        var O = Object(this);
        var len = parseInt(O.length, 10) || 0;
        if (len === 0) {
    ...
    
  7. includes(searchElement)
    Array.prototype.includes = function(searchElement) {  
        var O = Object(this);
        var len = parseInt(O.length) || 0;
        if (len === 0) {
          return false;
        var n = parseInt(arguments[1]) || 0;
        var k;
        if (n >= 0) {
    ...
    
  8. includes(target)
    Array.prototype.includes = function (target) {
      var result = false
      this.forEach(function (el) {
        if (el == target) {
          result = true
      });
      return result;
    
  9. includes(target)
    Array.prototype.includes = function(target) {
      for(var i in this) {
        if(this[i] === target) {
          return true;
        };
      };
      return false;
    };