Nodejs Array Search search(value)

Here you can find the source of search(value)

Method Source Code

Array.prototype.search = function(value){
 // var this = [];
  //this = this.toTwenty();
  var startIndex  = 0,
  stopIndex = this.length - 1,//from w  w w  . j a v  a  2  s  . co m
  middle = Math.floor((stopIndex + startIndex)/2);
  var count = 0;
  while(this[middle] != value && startIndex < stopIndex){
    
    if (value < this[middle]){
      stopIndex = middle - 1;
    }else if (value > this[middle]){
      startIndex = middle + 1;
    }
    middle = Math.floor((stopIndex + startIndex)/2);
   count+=1;
  } 
  var l = 0;
  l = this.length;
  var index = (this[middle] != value) ? -1 : middle;
  return {index:index, count :count, Length : l}
}

module.exports = Array.prototype;

Related

  1. search(key, value)
    Array.prototype.search = function (key, value) {
        return this.reduce(function (result, el) {
            if (result) return result;
            if (key) {
                if (typeof key === "function") {
                    if (key(el) === value) {
                        return el;
                } else {
    ...
    
  2. search(key, value)
    Array.prototype.search = function (key, value) {
        if (arguments.length == 1) {
            return this.filter(function (e) {
                return e == value;
            });
        if (arguments.length == 2) {
            var k = arguments[0];
            var value = arguments[1];
    ...
    
  3. search(n)
    Array.prototype.search = function(n) {
      var start = 0;
      var end = this.length - 1;
      var result = {count: 0, index: -1, length: this.length};
      while (start <= end) {
        mid = (start + end) / 2 >> 0;
        if (this[start] === n) {
          result.index = start;
          return result;
    ...
    
  4. search(search_func)
    Array.prototype.search = function(search_func){
        for (var i= 0; i<this.length; i++){
            if (search_func(this[i]) == 0)
                return this[i]
        return null;
    
  5. search(v)
    Array.prototype.search = function(v){
      for (i=0;i<this.length;i++) {
        if (this[i]===v) return i;
    
  6. search(value)
    Array.prototype.search = function(value){
      var startIndex  = 0,
      stopIndex = this.length - 1,
      middle = Math.floor((stopIndex + startIndex)/2);
      var count = 0;
      while(this[middle] != value && startIndex < stopIndex){
        count+=1;
        if (value < this[middle]){
          stopIndex = middle - 1;
    ...
    
  7. searchById(id)
    Array.prototype.searchById = function(id){
      for(var i=0; i<this.length; i++){
        if(this[i].id === id){
          return this[i];
    };
    
  8. searchIndex(n)
    "use strict";
    Array.prototype.searchIndex = function(n){
      var result = [],
          i,
          l= this.length;
      for( i= 0; i< l; i++){
        if(this[i] == n){
          result.push(i);
      if(result.length == 0){
        result.push(-1);
      return result;
    };
     var arr = [1,2,5,5,5,5,5,6,9];
    console.log(arr.searchIndex(5));
    console.log(arr.searchIndex(0));
    
  9. searchObject(filterParams)
    'use strict';
    Array.prototype.searchObject = function (filterParams) {
        return this.filter(function (v) {
            return recursiveMatchFunc(v, filterParams);
        });
    };
    Array.prototype.randomSelect = function () {
        return this[Math.floor(Math.random() * this.length)];
    };
    ...