[JavaScript]扩展运算符

顺便补充一点Array.reduce((prev, curv) => { },  ori) 的运用

        let arr1 = [1, 3, 5, 7, 9]
        let arr2 = [2, 4, 6, 8, 10]
        console.log(...arr1); //展开一个数组       -----> 1 3 5 7 9
        let arr3 = [...arr1, ...arr2]//连接数组

        //在函数中使用
        function sum(...numbers) {
            return numbers.reduce((preValue, currentValue) => {
                return preValue + currentValue
            })
        }
        console.log(sum(1, 2, 3, 4)); //        -----> 10

  

        let arr1 = [1, 3, 5, 7, 9]
        let arr2 = [2, 4, 6, 8, 10]
        console.log(...arr1); //展开一个数组
        let arr3 = [...arr1, ...arr2]//连接数组

        //在函数中使用
        function sum(numbers) {
            return numbers.reduce((preValue, currentValue) => {
                return preValue + currentValue
            })
        }
        console.log(sum(arr3));//             -----> 55

 

// 构造字面量对象时使用展开语法
let person = {name:'tom',age:18}
let person2 = {...person}
// console.log(...person); //报错,展开运算符不能展开对象
// TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function
person.name = 'jerry' console.log(person2); // ----> {name: 'tom', age: 18} console.log(person); // ----> {name: 'jerry', age: 18}

 

posted @ 2023-10-13 14:34  夕苜19  阅读(9)  评论(0编辑  收藏  举报