Conditional spread element

后端 未结 5 2134
無奈伤痛
無奈伤痛 2021-02-19 03:50
const cond = false

const extraInfo = [
  {
    a: 11,
    b: 25
  },
  {
    a: 12,
    b: 34
  },
  {
    a: 1,
    c: 99
  }
]

const userInfo = [
  {
    z: 8
  },
          


        
5条回答
  •  星月不相逢
    2021-02-19 03:54

    Conditionally spread an entity to Object

    console.log(
      { 
        name: 'Alex',
        age: 19,
        ...(true && { city: 'Kyiv' }),
        ...(false && { country: 'Ukraine' })
      }
    ) 
    
    // { name: 'Alex', age: 19, city: 'Kyiv' }
    
    

    Conditionally spread an entity to Array

    
    console.log(
      [
        'Dan',
        'Alex',
        ...(true ? ['Robin'] : [])
      ]
    )
    
    // [ 'Dan', 'Alex', 'Robin' ]
    
    

提交回复
热议问题