小程序push怎么用

小程序中push()方法可向数组的末尾添加一个或多个元素,并返回新的长度。push遇到数组参数时,把整个数组参数作为一个元素。

语法

arrayObject.push(newelement1,newelement2,....,newelementX)

push和concat二者功能很相像:

var arr = [];
arr.push(1);
arr.push(2);
arr.push([3, 4])
arr.push(5, 6);
arr = arr.concat(7);
arr = arr.concat([8, 9]);
arr = arr.concat(10, 11);
for(var i in arr){
  console.log(i+-----+arr[i]);
}

打印结果如下:

index.js [sm]:180 0-----1

index.js [sm]:180 1-----2

index.js [sm]:180 2-----3,4

index.js [sm]:180 3-----5

index.js [sm]:180 4-----6

index.js [sm]:180 5-----7

index.js [sm]:180 6-----8

index.js [sm]:180 7-----9

index.js [sm]:180 8-----10

index.js [sm]:180 9-----11

推荐:《小程序开发教程

相关文章

开发微信小程序的用户授权登录功能
小程序开发页面如何实现跳转?
浅谈小程序开发中蓝牙连接错误分析及解决方法
什么是小程序?它有哪些功能?
如何配置小程序开发项目结构?(教程)
怎么把自己的店加入小程序