1.push()向数组的尾部添加一个元素,有参数,返回新的数组元素,改变原数组
利用for循环遍历新增的元素,新增的元素都是存在类数组arguments里的的,所以健类数组里面的值赋值给数组
var numbers = [60, 66, 40, 88];
Array.prototype.myPush = function () {
console.log(this);
for (i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i];
}
return this.length
}
var res = numbers.myPush(23, 456);
console.log(res);
console.log(numbers);
2.pop()删除数组的最后一个元素,无参数,返回的是被删掉的元素,改变原数组
将数组的最后一个元素赋值给last作为返回值,然后数组的长度减一
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
3.unshift()向数组的最前面添加一个元素,有参数,返回的是新数组的长度,原数组被改变
首先定义一个新的数组,这个数组的长度是原数组长度加arguments的长度,然后从i=sum开始循环,将每一个原数组元素向后移动需要新增元素个数个位置,使新的元素有位置可以放,知道i的值小于等于arguments.length的值时就可以将arguments里面的值赋值给数组了
var arr = [1, 2, 3, 4, 5];
Array.prototype.unShift = function () {
sum = this.length + arguments.length;
for (i = sum; i > 0; i--) {
if (i > arguments.length) {
this[i - 1] = this[i - 1 - arguments.length];
} else {
this[i - 1] = arguments[i - 1]
}
}
return sum
}
var res = arr.unShift(5, 7);
console.log(res, arr);//7 [1,2,3,4,5,5,7]
4.shift()删除数组的第一个元素,无参数,返回被删掉的值,原数组被改变
var arr = [1, 2, 3, 4, 5];
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,5]
5.some()遍历数组,只要有一个符合条件就返回true,否则返回false
var arr = [1, 2, 3, 4];
Array.prototype.mySome = function (fun, obj) {
for (let i = 0; i < this.length; i++) {
// 只要有一个满足条件 返回true
// call apply 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);
6.every()遍历数组,只要有一个不符合条件就返回false,否则true
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) {
// every 只要有一个不满足条件 跳出循环 不再向下判断
// console.log('1111');
console.log(this);//this -global
// 只要写了第二个参数 this--->第二个参数
return item > 1
}, 'hello');
console.log(res, arr);
7.map对每一个数组做操作,返回新的数组
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]) : fun(this[i]))
}
return newArr
}
var res = arr.myMap(function (item, index, arr) {
console.log(this);
return item + 1
}, { name: "zhangsan" });
console.log(res); //[2,3,4,5]
8.filter() 过滤符合条件的数组元素组成新数组
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]) : 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); //[3,4]
9.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);
console.log(item);
}, { name: "zhangsan" });
console.log(res);