Nodejs Array Index indexOfObject(prop, val)

Here you can find the source of indexOfObject(prop, val)

Method Source Code

Array.prototype.indexOfObject = function(prop, val) {
   function traverseObject(obj) {
      if(typeof(obj) === 'object') {
         for(var e in obj) {
            if(obj[prop] == val) {
               return true;
            } else {/* w  ww .j  a v a  2s  . c  o  m*/
               return traverseObject(obj[e]);
            }
         }
      }
      return false;
   }
   for(var i = 0; i < this.length; i++) {
      if(this[i] == val) {
         return i;
      } else if(traverseObject(this[i])) {
         return i;
      }
   }
   return -1;
};

Related

  1. indexOfLeastGreaterThan(val)
    Array.prototype.indexOfLeastGreaterThan = function(val) {
      var minIndex = 0;  
      var minDist = Number.MAX_VALUE;
      for (var i = 0; i < this.length; i++) {
        if ((this[i] >= val) && (Math.abs(this[i] - val) < minDist)) {
          minDist = Math.abs(this[i] - val);
          minIndex = i;
      return minIndex;
    
  2. indexOfMatchFunction(func)
    Array.prototype.indexOfMatchFunction = function(func) {
        "use strict";
        for (var i in this) {
            if (!this.hasOwnProperty(i))
                continue;
            var element = this[i];
            if (func(element))
                return parseInt(i);
        return -1;
    };
    
  3. indexOfObject(key, value)
    Array.prototype.indexOfObject = function(key, value)
        if(this.length == 0 ) return -1;
        for( var i = 0; i < this.length; i++)
            if(this[i][key] == value)
                return i;
        return -1;
    };
    
  4. indexOfObject(myArray, searchTerm, property)
    Array.prototype.indexOfObject = function (myArray, searchTerm, property) {
        for(var i = 0, len = myArray.length; i < len; i++) {
            if (myArray[i][property] === searchTerm) 
            return i;
        return -1;
    
  5. indexOfObject(obj)
    Array.prototype.indexOfObject = function(obj) {
      for ( var i = 0, len = this.length; i < len; i++) {
        if (angular.equals(this[i], obj))
          return i;
      return -1;
    };
    
  6. indexOfObject(property, value)
    Array.prototype.indexOfObject = function(property, value) {
        for (var i = 0, len = this.length; i < len; i++) {
            if (this[i] !== null && this[i][property] === value) return i;
        return -1;
    
  7. indexOfObjectIdindexOfObjectId(element)
    Array.prototype.indexOfObjectId = function indexOfObjectId(element) {
        for (var i = 0, len = this.length; i < len; i++) {
            if (this[i].equals(element)) {
                return i;
        return -1;
    };
    
  8. indexOfSelect(filter)
    Array.prototype.indexOfSelect = function(filter)
      for (var i = 0; i< this.length; i++) {
        if(filter(this[i])){
          return i;
      return -1;
    
  9. indexOfSmallestindexOfSmallest(k)
    Array.prototype.indexOfSmallest = function indexOfSmallest(k) {
      let lowest = 0;
      const a = this;
      for (let i = 1; i < a.length; i++) {
        if (k) {
          if (a[i][k] < a[lowest][k]) lowest = i;
        } else {
          if (a[i] < a[lowest]) lowest = i;
      return lowest;
    };