1.push()方法,向数组末尾添加一个或多个元素
var arr = [1, 2, 3, 4, 5]
Array.prototype.myPush = function() {
for (var i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i]
// 数组最后一项的下一项
}
return this.length
}
arr[arr.length] = 6
// 添加单个元素
console.log(arr);
arr.myPush(7, 8)
// 调用函数添加多个元素
console.log(arr);
2.pop()方法,删除数组最后一个元素并返回其值
var arr = [1, 2, 3, 4, 5]
Array.prototype.myPop = function() {
var lastItem = this[this.length - 1]
// 获取数组最后一个元素
this.length--
// 删除元素后对数组逐渐递减
return lastItem
}
var itemDel = arr.myPop()
// 调用函数并打印新数组
console.log(arr);
console.log(itemDel);
// 打印删除元素
3.shift()方法,删除数组第一个元素并返回第一个元素的值
var arr = [1, 2, 3, 4, 5]
Array.prototype.shift = function() {
for (var i = 0; i < this.length; i++) {
this[i] = this[i + 1]
//将数组的元素全部向前移动一位
}
this.length--
var firstItem = this[0]
return firstItem
}
var itemFirst = arr.shift()
console.log(arr);
console.log(itemFirst);
4.unshift()方法,向数组开头添加一个或者多个元素并返回新数组长度
var arr = [1, 2, 3, 4, 5];
Array.prototype.myUnshift = function() {
// 计算新数组长度
let sum = this.length + arguments.length
for (let i = sum; i > 0; i--) {
// 将原数组的元素向后移动auguments.length
// 注意arguments.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', 6);
console.log(res, arr);
5.every()方法,筛选数组中符合指定条件的元素
var arr = [1, 2, 3, 4,5];
Array.prototype.myEvery = function(fun, obj) {
var res = true
for (let i = 0; i < this.length; i++) {
// 三目运算符 obj为false执行:后面的,true执行前面
res = obj ? fun.call(obj, this[i], i, this) : fun(this[i], i, this)
// call修改this指向 还可以用apply bind
if (!res) {
return false
}
}
return true
}
var res = arr.myEvery(function(item, index, arr) {
//有第二个参数的情况
console.log(this)
return item > 0
//指定条件
}, 'hello');
console.log(res, arr);
6.some()方法,有一个元素满足条件就返回true,没有为false
var arr = [1, 2, 3, 4, 5];
Array.prototype.mySome = function(fun, obj) {
for (let i = 0; i < this.length; i++) {
// 只要有一个满足条件 返回true
// call apply bind方法都行 这里用bind
if (obj ? fun.bind(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);
return item > 1
}, { name: "zhangsan" });
console.log(res);
7.filter()方法,筛选符合条件的元素组成新数组
var arr = [1, 2, 3, 4, 5];
Array.prototype.myFilter = function(fun, obj) {
let newArr = [];
for (let i = 0; i < this.length; i++) {
if (obj ? fun.call(obj, this[i]) : fun(this[i])) {
newArr.push(this[i])
}
}
return newArr;
}
var res = arr.myFilter(function(item, index, arr) {
console.log(this);
return item > 2
}, { name: "zhangsan" });
console.log(res, arr);
8.map()方法,返回一个新数组,数组中的元素为原本数组调用函数处理后的值
var arr = [1, 2, 3, 4, 5]
Array.prototype.myMap = function(fun, obj) {
let newArr = []
for (let i = 0; i < arr.length; i++) {
// newArr.push(fun(this[i]))
newArr.push(obj ? fun.call(obj, this[i]) : fun(this[i]))
}
return newArr
}
var res = arr.map(function(item, index, arr) {
console.log(this);
return item + 1
}, { name: 'zhangsan' })
console.log(res);
9.forEach()方法,调用数组每个元素并将元素传递给回调函数
var arr = [1, 2, 3, 4, 5];
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);
console.log(item);
}, { name: "zhangsan" });
console.log(res);