目录
栈与队列方法
改变数组的长度。
Array.prototype.pop()
pop() 方法用于删除数组的最后一个元素并返回删除的元素。
//第一种方法
// 1.重构pop
// 参数:无 返回值:删除的数组元素,是否修改原数组:修改
var arr = [1, 2, 3, 4];
Array.prototype.myPop = function () {
// 考虑数组为空
if (this.length > 0) {
let last = this[this.length - 1];
this.length--;
return last;
} else {
return undefined;
}
}
var res = arr.myPop();
console.log(res, arr);//4 [ 1, 2, 3 ]
//第二种方法
// pop 删除数组的最后一个元素,并返回这个元素
var numbers = [2, 8, 56, 5, 11];
Array.prototype.myPop = function () {
arr = [];
var num = this[this.length - 1];
for (let i = 0; i < this.length - 1; i++) {
arr[arr.length] = this[i];
}
this.length = arr.length;
return num;
}
console.log(numbers.myPop()); //11
console.log(numbers); //[ 2, 8, 56, 5 ]
Array.prototype.push()
push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。
// 2.重构push方法
// 参数:任何想要添加的数据类型 返回值:新数组的长度,修改原数组
var arr=[1,2,3,4];
Array.prototype.myPush=function(){
for(let i=0;i<arguments.length;i++){
this[this.length]=arguments[i];
}
return this.length;
}
var res=arr.myPush('hello',12,57);
console.log(res,arr); //7 [ 1, 2, 3, 4, 'hello', 12, 57 ]
Array.prototype.shift()
shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。
// 3.重构shift方法
// 参数:无 返回值:删除掉的数组元素 修改原数组
var arr = [1, 2, 3, 4];
Array.prototype.myShift = function () {
let first = this[0];
for (let i = 0; i < this.length; i++) {
this[i] = this[i + 1];
}
this.length--;
return first;
}
var res = arr.myShift();
console.log(res, arr); //1 [ 2, 3, 4 ]
Array.prototype.unshift()
unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。
//第一种方法
// 4.重构unshift方法
// 参数:任何想要添加的数组元素 返回值:新的数组长度,修改原数组
var arr = [1, 2, 3, 4];
Array.prototype.myUnshift = function () {
// 计算新数组长度
let sum = this.length + arguments.length;
for (let i = sum; i > 0; i--) {
// 将原数组的元素向后移动 auguments.length
if (i > arguments.length) {
this[i - 1] = this[i - 1 - arguments.length];
} else {
this[i - 1] = arguments[i - 1];
}
}
return sum;
}
var res = arr.myUnshift('hello', 45);
console.log(res, arr); //6 [ 'hello', 45, 1, 2, 3, 4 ]
//第二种方法
// unshift 向数组的开头添加一个或多个元素,并返回新的长度
var numbers = [2, 8, 56, 5, 11];
Array.prototype.myUnshift = function () {
temp = [];
for (var i = 0; i < arguments.length; i++) {
temp[temp.length] = arguments[i];
}
for (var j = 0; j < this.length; j++) {
temp[temp.length] = this[j];
}
this.length = temp.length;
for (var k = 0; k < temp.length; k++) {
this[k] = temp[k];
}
return this.length;
}
console.log(numbers.myUnshift(1, 10, 111)); //8
console.log(numbers);
// [
// 1, 10, 111, 2,
// 8, 56, 5, 11
// ]
五个迭代方法
不改变数组长度
Array.prototype.every()
every() 方法用于检测数组所有元素是否都符合指定条件(通过函数提供)。
// 1.every
// 参数:函数(item,index,arr) 返回值:true 或者false;
var arr = [1, 2, 3, 4];
Array.prototype.myEvery = function (fun, obj) {
for (let i = 0; i < this.length; i++) {
if (!(obj ? fun.call(obj, this[i], i, this) : fun(this[i], i, this))) {
return false;
}
}
return true;
}
var res = arr.myEvery(function (item, index, arr) {
// 只要有一个不满足条件,跳出循环 不再向下判断
console.log(this);// 四次[String: 'hello']
// 只要写了第二个参数 this-->第二个参数
return item > 0;
}, ('hello'))
console.log(res); //true
Array.prototype.some()
some() 方法用于检测数组中的元素是否满足指定条件(函数提供)。
// 2.some
// 只要有一个满足一个条件 返回true 跳出循环
var arr = [1, 2, 3, 4];
Array.prototype.mySome = function (fun, obj) {
for (let i = 0; i < this.length; i++) {
if (obj ? fun.call(obj,this[i],i,this):fun(this[i],i,this)){
return true;
}
}
return false;
}
var res = arr.mySome(function (item, index, arr) {
console.log(this); //两次zhangsan
return item > 1;
}, { name: "zhangsan" });
console.log(res); //true
Array.prototype.filter()
filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
// 3.filter 过滤符合条件的数组元素组成新的数组
// 返回值:新数组 参数(item,index,arr),this指向变化
var arr = [1, 2, 3, 4];
Array.prototype.myFilter = function (fun, obj) {
let newArr = [];
for (let i = 0; i < this.length; i++) {
if (obj ? fun.call(obj, this[i],i,this) : fun(this[i],i,this)) {
newArr.push(this[i]);
}
}
return newArr;
}
var res = arr.myFilter(function (item, index, arr) {
console.log(this); //四次zhangsan
return item > 2;
}, { name: "zhangsan" });
console.log(res); //[3,4]
Array.prototype.map()
map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。
// 4.map 映射对每一个数组元素操作
// 参数:函数(item,index,arr) 返回值:操作后的新数组
var arr = [1, 2, 3, 4];
Array.prototype.myMap = function (fun, obj) {
let newArr = [];
for (let i = 0; i < this.length; i++) {
newArr.push(obj ? fun.call(obj, this[i], i, this) : fun(this[i], i, this))
}
return newArr;
}
var res = arr.myMap(function (item, index, arr) {
console.log(this); //四次zhangsan
return item + 1;
}, { name: "zhangsan" });
console.log(res); //[ 2, 3, 4, 5 ]
Array.prototype.forEach()
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。
// 5.forEach 遍历数组
var arr = [1, 2, 3, 4];
Array.prototype.myForEach = function (fun, obj) {
for (let i = 0; i < this.length; i++) {
obj ? fun.call(obj, this[i]) : fun(this[i])
}
}
var res = arr.myForEach(function (item, index, arr) {
// console.log(this); //四次zhangsan
console.log(index + ' -- ' + item);
// undefined -- 1
// undefined -- 2
// undefined -- 3
// undefined -- 4
}, { name: "zhangsan" });