Nodejs Array Find findRanges()

Here you can find the source of findRanges()

Method Source Code

/*//from w ww.  j a  va 2 s .  com
  Array.findRanges
  
  Turns this:
  
  ["a","a","a","b","b","c","c","c","c","c","a","a","c"]
  
  into this:
  
  {
    "a":[
      {
        "from":0,
        "to":2
      },
      {
        "from":10,
        "to":11
      }
    ],
    "b":[
      {
        "from":3,
        "to":4
      }
    ],
    "c":[
      {
        "from":5,
        "to":9
      },
      {
        "from":12,
        "to":12
      }
    ]
  }

*/

Array.prototype.findRanges = function() {
  var buckets = {};
  for(var i = 0; i < this.length; i++) {
    if(!(this[i] in buckets)) {
      buckets[this[i]] = [{
        from: i,
        to: i
      }]
    } else {
      var last = buckets[this[i]][ buckets[this[i]].length-1 ];
      if(i == last.to + 1) {
        last.to = i;
      } else {
        buckets[this[i]].push({
          from: i,
          to: i
        })
      }
    }
  }
  return buckets;
};
Array.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {         
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};
Object.values = function(obj){ return Object.keys(obj).map(function(key){return obj[key]}) };

Related

  1. findHighestNumber(inField)
    Array.prototype.findHighestNumber = function(inField)
      var tmp = [];
      for (var i = 0; i < this.length; i++) {
        tmp[i] = Number(this[i][inField]);
      if(this.length < 1)
        return 0;
      return Math.max.apply(Math, tmp);
    ...
    
  2. findIdx(fn)
    Array.prototype.findIdx = function(fn) {
        return this.indexOf(this.filter(val => fn(val))[0]);
    };
    
  3. findIfNotError(filter)
    Array.prototype.findIfNotError = function(filter) {
        var result = this.find(filter);
        if ( !result ) {
            throw new Error();
        return result;
    
  4. findInJson(attr, value)
    Array.prototype.findInJson = function (attr, value){
      for(var i=0; i<this.length; i++){
        this[i][attr] = this[i][attr].toLowerCase();
        if(this[i][attr] == value){
          return true;
        };
      };
      return false;
    };
    ...
    
  5. findMaximumfindIt()
    var array = [47, 98, 0, -23, 4, 87.3, 0.0003, +9];
    Array.prototype.findMaximum = function findIt() {
    var arrayValue = 0;
    for(var i = 0; i < array.length; i++) {
      if (array[i] > arrayValue) {
        arrayValue = array[i];
      return arrayValue;
    ...
    
  6. findThing(n)
    Array.prototype.findThing = function(n){
      for(var i=0; i<this.length; i++){
        if(this[i] === n){
          return true;
        };
      };
      return false;
    };
    
  7. findYoungestPersonfindYoungestPerson()
    Array.prototype.findYoungestPerson = function findYoungestPerson() {
            var youngest = this[0];
        for (var person = 1; person < this.length; person += 1) {
            if (this[person].age < youngest.age) {
                youngest = this[person];
        return youngest;
    };
    ...
    
  8. find_by(prop, val)
    Array.prototype.find_by = function(prop, val) {
      return this.filter(function(obj) {
        return obj[prop] === val
      }).first()