//数组的toString()
var arr = ["abc","def","ghi"]
//toString()方法把数组转换为逗号相隔的字符串
var newarr = arr.toString()
console.log(newarr)//abc,def,ghi
var arr = ["aaa","bbb","ccc"]
//join()也是将数组转换为字符串,但是可以更改分隔符,默认是(,)
var newarr = arr.join()
var newarr2 = arr.join("")
var newarr3 = arr.join("!")
console.log(newarr)//aaa,bbb,ccc
console.log(newarr2)//aaabbbccc
console.log(newarr3)//aaa!bbb!ccc
var arr = ["red","green","blue"]
//splice(index,num,add) index是确定数组的下标(从哪开始),num是删除的数量,add选填,添加的新元素,返回被删除的元素数组
console.log(arr.splice(1,2))//["green","blue"]
console.log(arr)//["red"]
var arr = ["cat","dog","kola"]
//slice(start,end) 返回一个新的数组,包含从start到end(不包括该元素)的新数组,如果end未被规定,那么会选取从start到数组结尾的所有元素。
var newarr = arr.slice(1)
console.log(newarr)//["dog", "kola"]
var arr = ["a","b","c","d"];
var arr2 = ["e","f","g"]
//concat() 法通过合并现有数组来返回一个新数组
var newarr = arr.concat(arr2)
console.log(newarr)//["a", "b", "c", "d", "e", "f", "g"]
玩一下数组的一些方法
最新推荐文章于 2024-08-19 10:59:54 发布