数组方法合集

1、数组的删除和增加方法

  1. push()
    push方法是在数组的最后面添加一项或者多项,push方法会改变原本的数组
    返回值:更新后新数组的长度,而不是更新后的数组。
   let a = [1,2,3,4,5]
   let b = a.push(6)
   console.log(a);//[1, 2, 3, 4, 5, 6]
   console.log(b);//6
  1. pop()
    pop方法是删除数组的最后一项,没有参数会改变原数组
    返回值:是被删除的元素(当数组为空时返回undefined)
	let a = [1,2,3,4,5]
    let b = a.pop()
    console.log(a);// [1, 2, 3, 4]
    console.log(b);// 5

push方法和pop方法的区别
push方法和pop方法一个是在数组最后添加元素和数组最后删除元素,两者还有区别就是push方法是有参数的而pop方法是没有参数的,push方法的返回值是更新后的数组长度,而pop方法返回的是被删除的元素

  1. unshift()
    unshift方法和push方法类似,unshift方法是在数组的前面来添加一个或者多个元素和push方法一样会改变原数组
    返回值:更新后的数组长度
	let a = [1,2,3,4,5]
    let b = a.unshift(6,7)
    console.log(a);//[6, 7, 1, 2, 3, 4, 5]
    console.log(b);//7
  1. shift()
    shift方法和pop方法类似,shift方法删除数组的第一个元素,会改变原数组的长度
    返回值:被删除的元素(当数组为空时返回undefined)
	let a = [1,2,3,4,5]
    let b = a.shift()
    console.log(a);//[2, 3, 4, 5]
    console.log(b);//1
  1. splice()
    可以对数组进行增、删、改动
    删除n项:arr.splice(起始位置,n)
    增加元素:arr.splice(起始位置,0,添加1,添加2…)
    修改元素:arr.splice(起始位置,2,替换1,替换2)
    返回值:被删除或者被修改元素集合的新数组
    增加元素和修改元素中起始位置后面的数字代表所要修改的数的个数,0代表不修改只增加
    splice()有3个参数前2个是必须,第一个参数整数,规定添加/删除项目的位置,使用负数可从数组结尾处规定位置
    第二个参数要删除的项目数量。如果设置为 0,则不会删除项目
    第三个参数,替换的元素
      //删除         
      var a = [1,5,8,7,6]         
      var b = a.splice(2,1)         
      console.log(b);//[8]         
      console.log(a);//[1, 5, 7, 6] 
      //增加         
      var a = [1,5,8,7,6]         
      var b = a.splice(2,0,1)         
      console.log(b);//[]         
      console.log(a);//[1, 5, 1, 8, 7, 6] 
      //修改         
      var a = [1,5,8,7,6]         
      var b = a.splice(2,2,99,77)         
      console.log(b);//[8, 7]         
      console.log(a);// [1, 5, 99, 77, 6]

2、数组常用的一些循环方法

  1. forEach
    forEach方法对数组的每个元素执行一次给定的函数
    forEach方法的参数是一个函数,而函数中还有4个参数,分别是循环的数组的每一项,循环的元素下标,数组本身,和执行回调函数时的this
    参数:arr.forEach(callback(currentValue [, index [, array]])[, thisArg])
    callback
    为数组中每个元素执行的函数,该函数接收一至三个参数:
    currentValue
    数组中正在处理的当前元素。
    index 可选
    数组中正在处理的当前元素的索引。
    array 可选
    forEach() 方法正在操作的数组。
    thisArg 可选
    可选参数。当执行回调函数 callback 时,用作 this 的值
    返回值:undefined
	let a = [1,2,3,4,5]
    let b = a.forEach(item => {
      item = item + 1
      console.log(item)//2,3,4,5,6;
    })
    console.log(a)//[1,2,3,4,5];
    console.log(b)//undefined;

forEach方法也可以实现执行指定函数后改变数组

	let a = [1,2,3,4,5]
    let b = a.forEach((item,index,arr) => {
      arr[index] = item + 1
    })
    console.log(a)//[2, 3, 4, 5, 6];
    console.log(b)//undefined;
  1. map()
    map方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值
    map的参数和forEach一样区别在与map方法会返回一个符合函数逻辑后的新数组
	let a = [1,2,3,4,5]
    let b = a.map(item => {
      if ( item === 3) {
        return  item = 'three'
      } else {
        return item
      }
    })
    console.log(a)//[1, 2, 3, 4, 5];
    console.log(b)//[1, 2, "three", 4, 5];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值