Javascript Array newJoin(str)
Array.prototype.newJoin = function (str) { var res = ''; for (let i = 0; i < this.length; i++) { if (i === this.length - 1) { res += this[i];//from w ww .j av a 2s . co m } else { res += this[i] + str; } } return res; }
Array.prototype.newJoin = function (str) { var times = this.length - 1; var res = ''; for (let i = 0; i < times; i++) { this.splice(i * 2 + 1, 0, str);//from w w w . j av a 2s. c om } for (let i = 0; i < this.length; i++) { res += this[i]; } return res; } var arr = [1, 2, 3]; console.log(arr.newJoin('x'));