Nodejs Array Index indexOf(searchElement)

Here you can find the source of indexOf(searchElement)

Method Source Code

/**//from  w w w  . j  ava  2  s  .  co  m
 * Array.prototype.indexOf Polyfill
 */

Array.prototype.indexOf = function(searchElement) {
   "use strict";
   if (this === void 0 || this === null) {
      throw new TypeError();
   }
   var t = Object(this);
   var len = t.length >>> 0;
   if (len === 0) {return -1;}
   var n = 0;
   if (arguments.length > 0) {
      n = Number(arguments[1]);
      // shortcut for verifying if it's NaN
      if (n !== n) {
         n = 0;
      } else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
         n = (n > 0 || -1) * Math.floor(Math.abs(n));
      }
   }
   if (n >= len) {return -1;}
   var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
   for (; k < len; k++) {
      if (k in t && t[k] === searchElement) {
         return k;
      }
   }
   return -1;
};

/* End of file indexof.js */

Related

  1. indexOf(obj, start)
    'use strict';
    Array.prototype.indexOf = function(obj, start) {
       for (var i = (start || 0), j = this.length; i < j; i++) {
           if (this[i] === obj) { 
               return i; 
       return -1;
    
  2. indexOf(object)
    Array.prototype.indexOf = function(object) {
      for (var i = 0, length = this.length; i < length; i++)
        if (this[i] == object) return i;
      return -1;
    };
    
  3. indexOf(s)
    Array.prototype.indexOf = function(s)
      for (var i = 0; i < this.length; ++i )
        if(this[i] == s) return i;
      return -1;
    };
    
  4. indexOf(s)
    Array.prototype.indexOf = function(s) {
      if(undefined!=s.position){
        for (var i = 0; i < this.length; i++) {  
              if (s.position == this[i].position)  
                  return i;  
      }else{
        for (var i = 0; i < this.length; i++) {  
              if (s == this[i])  
    ...
    
  5. indexOf(searchElement /*, fromIndex */)
    if (!Array.prototype.indexOf) {
      Array.prototype.indexOf = function (searchElement ) {
        'use strict';
        if (this == null) {
          throw new TypeError();
        var n, k, t = Object(this),
          len = t.length >>> 0;
        if (len === 0) {
    ...
    
  6. indexOf(searchElement, fromIndex)
    Array.prototype.indexOf = function(searchElement, fromIndex) {
      if (!fromIndex || !isFinite(fromIndex)) {
        fromIndex = 0;
      for (var i = fromIndex, l = this.length; i < l; ++i) {
        if (this.hasOwnProperty(i) && this[i] === searchElement) {
          return i;
      return -1;
    
  7. indexOf(showMe)
    Array.prototype.indexOf = function(showMe){
        for(identification in this){
            if(this[identification] == showMe){return identification}
        return undefined
    Array.prototype.max = function(){
      var max = 0;
      for(num in this){
    ...
    
  8. indexOf(src, form)
    'use strict';
    var arr = [
        'a', 'b', 'c', 'd', '1', 1, '0'
    ]
    console.log(arr.indexOf('b'));
    console.log(arr.indexOf(1, 3));
    console.log(arr.indexOf(13, 3));
    console.log(arr.indexOf('d'));
    Array.prototype.indexOf = function(src, form) {
    ...
    
  9. indexOf(substr,start)
    Array.prototype.indexOf=function(substr,start)
      var ta,rt,d='\0';
      if(start!=null){ta=this.slice(start);rt=start;}else{ta=this;rt=0;}
      var str=d+ta.join(d)+d,t=str.indexOf(d+substr+d);
      if(t==-1)return -1;rt+=str.slice(0,t).replace(/[^\0]/g,'').length;
      return rt;