Nodejs Array Object Key replace(oldObj, newObj)

Here you can find the source of replace(oldObj, newObj)

Method Source Code

Array.prototype.replace = function(oldObj, newObj){
   var indexOfOldItem = this.indexOf(oldObj)
   if(indexOfOldItem==-1){
      throw new Error("oldObj not in list")
   }//from w ww.  j a  va 2s. c om
   this[indexOfOldItem] = newObj
   return oldObj
};

Array.prototype._checkIndexInRange = function(index){
   if(index<0 || index>=this.length)
      throw new Error('IndexOutOfBounds')
};

Array.prototype.set = function(index, obj){
   this._checkIndexInRange(index)
   this[index] = obj
};

Array.prototype.contains = function(obj, compareFunc){
   if(compareFunc){
      return this.some(function(element, index, array){
         return compareFunc(obj, element)
      })
   }else{
      return this.indexOf(obj)!=-1
   }
};

Array.prototype.removeAtIndex = function(index){
   this._checkIndexInRange(index)
   var removedObj = this[index];
   return this.slice(0,index).concat(this.slice(index+1))
};

Related

  1. arrToObj()
    Array.prototype.arrToObj = function(){
      var obj = {};
      for (var i = 0; i < this.length; i++) {
        obj[i] = this[i]
      return obj;
    
  2. asObject(labels)
    Array.prototype.asObject = function(labels) {
      var result = {};
      var _ctx = this;
      labels.each(function(label, idx) {
        if (label != null)
          result[label] = _ctx[idx];
      })
      return result;
    
  3. destroy(obj)
    Array.prototype.destroy = function(obj){
      var destroyed = null;
      for(var i = 0; i < this.length; i++){
        while(this[i] === obj){
          destroyed = this.splice(i, 1)[0];
      return destroyed;
    
  4. fromObject(obj)
    Array.prototype.fromObject = function (obj) {
        var ar = [];
        for(item in obj){
            ar.push(obj[item]);
        return ar;
    };
    
  5. getAllObjectsBy(field, withValue)
    Array.prototype.getAllObjectsBy = function(field, withValue)
      var results = [];
      for (var i = 0; i < this.length; i++) {
        if(this[i][field] == withValue)
          results.push(this[i]);
      return (results.length>0) ? results : null;
    
  6. seen(obj)
    Array.prototype.seen = function (obj) {
      if (obj.type == 'notification') {
        var i = 0;
        while (this[i].noticeId != obj.noticeId && i < this.length)
          i++;
        return (i < this.length && this[i].seqN < obj.seqN);
      return false;
    
  7. singleRemove(objectToRemove)
    Array.prototype.singleRemove = function(objectToRemove) {
        if ((index = this.indexOf(objectToRemove)) !== -1) {
            this.splice(index,1);
        return this;