数组方法里push、pop、shift、unshift、join、split
分别是什么作用?
- push就是向数组后添加一个元素,数组的长度加1,返回值为数组长度
var arr=[1,2,3]
arr.push(5)
console.log(arr);//[1,2,3,5]
console.log(a.length);//4 - pop就是把数组的最后一个元素删除,返回值为最后一个元素
var arr=[1,2,3]
arr.pop()//删除[3]
console.log(arr)//[1,2]
console.log(arr.length)//3 - shift就是把数组的第一个元素删除,返回值为删除的元素
var arr=[1,2,3];
arr.shift();//删除[1]
console.log(arr)//[2,3]
console.log(arr.length)//2 - unshift就是在数组的第一个元素前添加一个新元素,返回值为数组长度
var arr=[1,2,3]
arr.unshift(10);//在1前添加元素10
console.log(arr);//[10,1,2,3]
console.log(arr.length)//4 - join 把使用参数作为链接符把数组的元素变成字符串拼接在一起,不会修改原数组内容。
var arr=["hello","css","html"]
console.log(arr.join("-"));//hello-css-html
console.log(arr.join(""))//hellocss*html - split 就是把字符串分割成数组,可自己选择分割的方式和长度
var str='hello world,it is amazing'
console.log(str.split(''))//["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", ",", "i", "t", " ", "i", "s", " ", "a","m","z","i","i","n","g"
console.log(str.split(' '))//['hello','world,it','is','amazing']
console.log(str.split(','))//['hello world','it is amazing']
console.log(str.split(',',1))['hello world']