Javascript Array insertSeparator(sep)

Description

Javascript Array insertSeparator(sep)


Array.prototype.insertSeparator = function (sep) {
  if (this === null || this.length < 2) return this;
  let result = new Array(this.length * 2 - 1);
  for (let i = 0; i < this.length; i++) {
    result[i * 2] = this[i];/*  w  w w  .  j a va 2  s  .  c  om*/
    if (i < this.length - 1) {
      result[i * 2 + 1] = sep;
    }
  }
  return result;
};

Javascript Array insertSeparator(sep)

Array.prototype.insertSeparator = function(sep) {
  if (this.length < 2) {
    return this;/*from  w  w  w .  j ava2s.  co m*/
  } else {
    var result = new Array(this.length * 2 - 1);
    for (var i = 0; i < this.length; i++) {
      result[i * 2] = this[i];
      if (i < this.length - 1) {
        result[i * 2 + 1] = sep;
      }
    }
    return result;
  }
}



PreviousNext

Related