1.复制数组--(Array.from(), 剩余参数)
//array.from()---将类数组对象转成数组
let a=[1,4,6,3,4]; let arr1=Array.from(a); arr1.push(9) let arr2=[...a]; arr2.push(8) console.log(a,arr1,arr2)
//[1, 4, 6, 3, 4] [1, 4, 6, 3, 4, 9] [1, 4, 6, 3, 4, 8]
2.Array.from()和Array.of()的用法
//Array.from--将类数组(有length的对象)转化为数组 //Array.of---将一组值转成数组 let a='apple'; console.log(Array.from(a)); //["a", "p", "p", "l", "e"] console.log(Array.of('aa','bb','cc')) //["aa", "bb", "cc"]
//改变原数组 let arr=[1,2,3,4,5,6]; arr.reverse(); console.log(arr) // [6, 5, 4, 3, 2, 1]
4.arr.sort()--数组元素排序
//改变原数组 let arr=[1,6,3,8,4,6,2]; arr.sort(); console.log(arr) //[1, 2, 3, 4, 6, 6, 8]
5.数组转字符串
//不改变原数组 let arr=[1,2,3,4,5,6]; arr.join(','); console.log(arr) console.log(arr.join(',')) // [1, 2, 3, 4, 5, 6] //1,2,3,4,5,6