Nodejs Array Index indexOfLeastGreaterThan(val)

Here you can find the source of indexOfLeastGreaterThan(val)

Method Source Code

/* /* w w  w.  j  ava2  s . co m*/
 * Return the index of the smallest element in the array
 * that is greater than or equal to val.
 * TODO: Be smarter if all elements are less than 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;
}

Related

  1. indexOfByProp(obj, property)
    Array.prototype.indexOfByProp = function(obj, property) {
      let result = -1;
      this.forEach(function(currentObj, index) {
        if (currentObj[property] === obj[property]) {
          result = index;
      });
      return result;
    };
    ...
    
  2. indexOfContent(searchTerm)
    Array.prototype.indexOfContent = function (searchTerm) {
      let index = -1;
      for (var i = 0, len = this.length; i < len; i++) {
        if (this[i].content == searchTerm) {
          index = i;
          break;
      return index
    ...
    
  3. indexOfElement(search, func)
    Array.prototype.indexOfElement = function(search, func) {
      for(var i = 0; i < this.length; i++) {
        var result = func(search, this[i]);
        if(result)
           return i;
      return -1;
    
  4. indexOfGreatestLessThan(val)
    Array.prototype.indexOfGreatestLessThan = 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;
    
  5. indexOfKey(key)
    Array.prototype.indexOfKey = function(key){
      for(var i in this) if(i == key) return i;
      return -1;
    
  6. 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;
    };
    
  7. 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;
    };
    
  8. 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;
    
  9. 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;
    };