join()
把数组的所有元素通过指定的分隔符进行分隔成字符串
不指定分隔符时,默认以英文逗号(,)分隔
var colors = ["red","green","blue","orange"]
console.log(colors.join()) // red,green,blue,orange
console.log(colors.join('-')) // red-green-blue-orange
push() 和 concat()
push() 在数组的末尾添加一个或者多个元素,并返回新的数组长度
concat() 首先会先创建当前数组的一个副本,然后添加新元素到副本的末尾,并返回新的数组
var colors = ["red", "blue"]
var length = colors.push("orange")
console.log(length) // length = 3
var newColors = colors.concat("green")
console.log(newColors) //red, blue, orange, green
pop()
把数组中的最后一个元素删除,并返回最后一个元素的值
可以和push()配合表现得像数据结构中的栈(一种后进先出的数据结构)
var colors = new Array()
var length = colors.push("red", "blue") //从后面添加两项返回数组的长度
console.log(length) // length = 2
var item = colors.pop() //移除数组中最后一项,返回最后一项的值,同时数组长度减1
console.log(item) //item = blue
shift()
把数组中的第一个元素删除,并返回第一个元素的值
可以和push()配合表现得像数据结构中的队列(一种先进先出的数据结构)
var colors = new Array()
var length = colors.push("red", "green", "blue") // 在数组后面添加三项
console.log(length) //length = 3
var item = colors.shift() //删除数组第一项 同时数组长度减1
console.log(item) // item = red