Array类型(数组项的添加和删除)

Array类型传送门:

声明与属性

添加与删除

数组项操作和位置查找
重排序
迭代与归并



转换方法(转换为字符串显示)

查看数组里面的值,可以调用toString()或valueOf()方法转换为字符串显示,默认为每个字符串之间以逗号分隔,也可以直接输出对象数组名进行查看


栈方法

  1. 栈是LIFO(Last-In-First-Out 后进先出)的数据结构,最新添加的项最早被移除
  2. push()方法从栈顶推入数据,pop()方法从栈顶移除数据
  •     push():可接收任意数量的参数,添加到数组末尾,返回的是修改后的数组长度
  •     pop():只能从数组末尾移除最后一项,减少length值,返回此移除的项
var colors = new Array();
alert(colors.push("red","blue"));           //2
alert(colors);                              //red,blue
alert(colors.push("green"));                //3
alert(colors);                              //red,blue,green

alert(colors.pop());                        //green
alert(colors);                              //red,blue
alert(colors.length);                       //2


队列方法


  1. 队列是FIFO(First-In-First-Out 先进先出)的数据结构
  • push()方法从末端添加数据
  • shift()方法从前端去除一个数据,减少length值,并返回该项
var colors = new Array();
alert(colors.push("red","blue"));           //2
alert(colors);                              //red,blue
alert(colors.push("green"));                //3
alert(colors);                              //red,blue,green

alert(colors.shift());                      //red
alert(colors);                              //blue,green
alert(colors.length);                       //2


unshift()与pop()方法

  1. 从相反的方向模拟队列
  • unshift()方法从前端添加任意项,并返回数组长度
var colors = new Array();
alert(colors.unshift("red","blue"));        //2
alert(colors);                              //red,blue
alert(colors.unshift("green"));             //3
alert(colors);                              //green,red,blue

alert(colors.pop());                        //blue
alert(colors);                              //green,red 


总结

用于添加和删除数组项可以使用的方法:

      数组前端添加:unshift()               前端删除:shift()

    末端添加: push()                          末端删除:pop()





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值