流浪のwolf

卷帝

导航

展开运算符在数组和对象中的使用

1. 数组中使用

1.1 合并2个数组

    const arr1 = [1, 2, 3]
    const arr2 = [4, 5, 6]
    console.log([...arr1, ...arr2])

1.2 求最值

 const arr1 = [1, 2, 3]
 const arr2 = [4, 5, 6]
 console.log(Math.max(...arr1))
 console.log(Math.min(...arr2))

1.3  把伪数组转换真数组

console.log(arguments) //  这是伪数组
const arr = [...arguments] // 真数组

2. 在对象中使用展开运算符

2.1 有条件添加对象属性

 let condition = true
      const obj = {
        name: '朱龙旭',
        ...(condition && { age: 24 }),
      }
      console.log(obj)

 

posted on 2022-08-22 16:11  流浪のwolf  阅读(33)  评论(0编辑  收藏  举报