spread operator = ... allows an iterable such as an array or string to be expanded into seperate elements. (unpacks the elements)
// “...” 展开运算符可以把一个可迭代对象(如数组、字符串)
// “展开(unpack)”成独立的元素。
// 常用于:
// 1. 在函数调用中传递多个参数
// 2. 复制数组
// 3. 合并数组
// 4. 拆解字符串
let numbers = [1, 2, 3, 4, 5];
// Math.max() 和 Math.min() 只能比较单独的数字,不能直接传数组
// 使用 “...” 展开运算符,把数组元素展开为单独的参数。
let maximum = Math.max(...numbers); // 相当于 Math.max(1,2,3,4,5)
let minimum = Math.min(...numbers); // 相当于 Math.min(1,2,3,4,5)
console.log(maximum); // 5
console.log(minimum); // 1Spread operator can also merge different arrays
// ======================== 合并多个数组 ========================
// 定义两个数组
let fruits = ["apple", "orange", "banana"];
let vegetables = ["carrots", "celery", "potatoes"];
// 使用展开运算符合并数组
// 你可以在中间或结尾添加其他元素
let foods = [...fruits, ...vegetables, "eggs", "milk"];
// 展开结果:["apple", "orange", "banana", "carrots", "celery", "potatoes", "eggs", "milk"]
console.log(foods);