1、扩展运算符
<script>
let arrays = [1, 2, "abc"];
console.log(...arrays);
</script>
2、替代apply方法
<script>
//从一个数组中找出最大的数
let arrays = [1, 2, 3, 4, 5];
//es5的写法
console.log(Math.max.apply(null, arrays));
//es6的写法
console.log(Math.max(...arrays));
</script>
3、数组合并
<script>
//合并数组
let arrays1 = [1, 2, 3, 4, 5];
let arrays2 = [6, 7, 8, 9, 10];
//es5的写法
console.log(arrays1.concat(arrays2));
//es6的写法
console.log([...arrays1, ...arrays2]);
</script>
4、Array.from方法
作用:将伪数组转为真正的数组
伪数组有:1、arguments 2、元素集合 3、类似数组的对象
<script>
// 1、arguments
function add(){
console.log(arguments);
console.log(arguments[0]);
console.log(arguments[1]);
console.log(arguments[2]);
//arguments.push(40); //error arguments.push is not a function
//把arguments转化为真正可操作的数组
let arrays = Array.from(arguments);
arrays.push(40);
arrays.push(50);
console.log(arrays);
}
add(10,20,30);
</script>
<body>
<div></div>
<div></div>
<div></div>
<script>
// 2、元素集合
let divs = document.getElementsByTagName("div");
console.log(divs);
//divs.push(10); //error divs.push is not a function
// 把元素集合转化为可操作的数组
let arrays = Array.from(divs);
arrays.push(10);
arrays.push(20);
console.log(arrays);
</script>
</body>
<script>
// 3、类似数组的对象
let user = {
"0":"azhi",
"1":18,
"2":"男",
length:3
}
let arrays = Array.from(user);
arrays.push(80);
console.log(arrays);
</script>
5、Array.of方法
将一组值转化为数组
<script>
let arrays = Array.of(1,2,3,4,5,6,7,8);
console.log(arrays);
</script>