重构数组方法api(pop push shift unshift every some filter map forEach)

push()方法
功能:向数组的末尾添加一个或多个元素

返回值:数组新长度

参数:item1, item2, …, itemx(必须。要添加到数组的元素)

Array.prototype.myPush = function () {
  for (var i = 0; i < arguments.length; i++) {
    this[this.length] = arguments[i]
  }
  return this.length
}
var numbers = [556, 1, 9, 16, 45, 55];
var res=numbers.myPush(123,456);
console.log(res);
console.log(numbers);

pop()方法
功能:删除数组的最后一个元素

返回值:删除的元素

参数:无

Array.prototype.myPop= function(){
    this.length-=1;
    return this.length
}
var numbers = [556, 1, 9, 16, 45, 55];
var res=numbers.myPop();
console.log(res);
console.log(numbers);

shift()方法
功能:删除数组的第一个元素

返回值:数组第一个元素的值

参数:无

Array.prototype.myShift= function(){
    first=this[0];
    for(var i=0;i<this.length;i++){
       this[i]=this[i+1];
    }
    this.length-=1;
    return first;
      }
var numbers = [556, 1, 9, 16, 45, 55];
var res=numbers.myShift();
console.log(numbers);

unshift()方法
功能:向数组的开头添加一个或更多元素

返回值:数组新长度

参数:item1,item2, …, itemx(可选。向数组起始位置添加一个或者多个元素)

Array.prototype.myunshift = function () {
  for (var j = 0; j < arguments.length; j++) {
    for (var i = this.length; i >= 0; i--) {
      this[i] = this[i - 1];
    }
    this[0] = arguments[arguments.length - 1 - j];
  }
  return this.length;
}
var numbers = [556, 1, 9, 16, 45, 55];
var res = numbers.myunshift(1, 2, 3);
console.log(res);
console.log(numbers);

every()方法
功能:检测数组所有元素是否都符合指定条件(通过函数提供)

返回值:布尔值,true/false

参数:array.every(function(currentValue,index,arr), thisValue)

Array.prototype.myevery = function (fn, obj) {
  for (var i = 0; i < this.length; i++) {
    if (!(obj ? fn.call(obj, this[i], i, this) : (fn(this[i], i, this)))) {
      return false;
    }
  }
  return true;
}
var numbers = [556, 1, 9, 16, 45, 55];
var res = numbers.myevery(function (age) {
  // console.log(arguments);
  // console.log(this);
  // console.log(age);
  return age > 8;
}, 'hello')
console.log(res);

some()方法
功能:检测数组中的元素是否满足指定条件(函数提供)

返回值:布尔值,true/false

参数:array.some(function(currentValue,index,arr),thisValue)

Array.prototype.mysome = function (fn, obj) {
  for (var i = 0; i < this.length; i++) {
    if ((obj ? fn.call(obj, this[i], i, this) : fn(this[i], i, this))) {
      return true;
    }
  }
  return false;
}
var numbers = [556, 1, 9, 16, 45, 55];
var res = numbers.mysome(function (age) {
  console.log(this);
  return age > 8;
},‘hello’)
console.log(res);

filter()方法
功能:创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素

返回值:返回数组,包含了符合条件的所有元素

参数:array.filter(function(currentValue,index,arr), thisValue)

Array.prototype.myfilter = function (fn, obj) {
  var result = [];
  for (var i = 0; i < this.length; i++) {
    if ((obj ? fn.call(obj, this[i], i, this) : fn(this[i], i, this))) {
      result.push(this[i]);
    }
  }
  return result;
}
var numbers = [556, 1, 9, 16, 45, 55];
var res = numbers.myfilter(function (item) {
  console.log(this);
  return item > 8;
}, 5)
console.log(res);

map()方法
功能:返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值

返回值:返回一个新数组

参数:array.map(function(currentValue,index,arr), thisValue)

Array.prototype.mymap = function (fn, obj) {
  var result = [];
  for (var i = 0; i < this.length; i++) {

    // result.push(fn(this[i]));
    result.push(obj ? fn.call(obj, this[i], i, this) : fn(this[i], i, this));

  }
  return result;
}
var numbers = [556, 1, 9, 16, 45, 55];
var res = numbers.mymap(function (number) {
  console.log(this);
  return Math.sqrt(number);
}, { name: 'zhangsan' });
console.log(res);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值