1.push往末尾添加一个元素
Array.prototype.myPush = function () {
for (var i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i];
}
return this.length;
}
2.pop删除末尾的一个元素
Array.prototype.myPop = function () {
var temp = this[this.length - 1];
delete this[this.length - 1];
this.length -= 1;
return temp;
}
3.unshift往数组前添加一个或多个元素
Array.prototype.myUnshift = function () {
if (arguments.length) {
for (var i = arguments.length - 1; i >= 0; i--) {
for (var j = this.length - 1; j >= 0; j--) {
this[j + 1] = this[j];
}
this[0] = arguments[i];
}
return this;
} else {
return this;
}
}
4.shift删除第一个元素
Array.prototype.myShift = function () {
var temp = this[0];
for (var i = 0; i < this.length; i++) {
this[i] = this[i + 1];
}
delete this[this.length - 1];
this.length -= 1;
return temp;
}
5.reverse数组逆转
Array.prototype.myReverse = function () {
var tempArray = [];
for (var i = this.length - 1; i >= 0; i--) {
tempArray.myPush(this[i]); //使用myPush
}
for (var j = 0; j < this.length; j++) {
this[j] = tempArray[j];
}
return this;
}
6.splice(n,m,x…) 第n位开始,删除m长度,并在切口里添加一个或多个元素,返回被切除的数组
Array.prototype.mySplice = function (n, m) {
if (arguments.length == 0) {
return this;
} else if (arguments.length == 1) {
return this[n];
} else if (arguments.length == 2) {
if (n + m <= this.length - 1) {
for (var j = n + m; j < this.length; j++) {
this[j - m] = this[j]
}
this.length -= m;
return this;
} else {
return this;
}
} else {
if (n + m <= this.length - 1) {
for (var j = n + m; j < this.length; j++) {
this[j - m] = this[j]
}
this.length -= m;
for (var i = arguments.length - 1; i >= 2; i--) {
for (var k = this.length - 1; k >= n; k--) {
this[k + 1] = this[k]
}
this[n] = arguments[i];
}
return this;
} else {
return this;
}
}
}
7.sort排序,自己封装的排序方法有很多,这里列举两种,全是从小到大排
(1)冒泡排序
Array.prototype.mySort_bubble = function () {
for (var i = this.length; i >= 0; i--) {
for (var j = 0; j < i; j++) {
if (this[j] > this[j + 1]) {
var temp = this[j];
this[j] = this[j + 1];
this[j + 1] = temp;
}
}
}
return this;
}
(2)选择排序
Array.prototype.mySort_choose = function () {
for (var i = 0; i < this.length; i++) {
var min = i;
for (var j = i + 1; j < this.length; j++) {
if (this[j] < this[min]) {
min = j;
}
}
var temp = this[min];
this[i] = this[min];
this[min] = temp;
}
}
8.concat数组拼接
Array.prototype.myConcat = function () {
var tempArray = this;
for (var i = 0; i < arguments[0].length; i++) {
tempArray.myPush(arguments[0][i]);
}
return tempArray;
}
9.toString 转化成一个字符
Array.prototype.myToString = function () {
var temp = "[";
for (var i = 0; i < this.length; i++) {
if (i != this.length - 1) {
temp = temp + this[i] + ",";
} else {
temp = temp + this[i] + "]";
}
}
return temp;
}
10.slice截取
/**
* (1)没有参数,截取整个数组
* (2)一个参数,从第n位截取到最后1位
* (3)两个参数,从第n位截取到第m位
*/
Array.prototype.mySlice = function () {
var temp = [];
if (arguments.length == 0) {
return this;
} else if (arguments.length == 1) {
for (var i = arguments[0]; i < this.length; i++) {
temp.myPush(this[i]);
}
} else {
if (arguments[0] < arguments[1]) {
for (var j = arguments[0]; j < arguments[1] + 1; j++) {
temp.myPush(this[j]);
}
} else {
return this;
}
}
return temp;
}
11.数组去重
Array.prototype.unique = function () {
var temp = {};
var array = [];
for(var i = 0;i<this.length;i++){
if(!temp[this[i]]){
temp[this[i]]=true;
array.myPush(this[i]);
}
}
return array;
}