Flatten array in place - Node.js Array

Node.js examples for Array:Flatten

Description

Flatten array in place

Demo Code


Array.fill = function(count, generator){
  var a = new Array(count)
  for(var i=0; i<count; i++){
    a[i] = generator(i)//from w w  w.j a  v a  2  s  .  com
  }
  return a
}

function isDefined(a){
  return (a !== null) && (a !== undefined)
}

Array.prototype.flattenInPlace = function(){
  var readIndex = 0,
    writeIndex = 0
  
  while(readIndex < this.length){
    var value = this[readIndex]
    
    if(isDefined(value)){
      this[writeIndex++] = this[readIndex++]
    } else {
      readIndex++
    }
  }

  this.length = writeIndex
}

Related Tutorials