Conditional spread element

后端 未结 5 2117
無奈伤痛
無奈伤痛 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' ]
    
    
    0 讨论(0)
  • 2021-02-19 03:54

    const extraInfo = [
      {
        a: 11,
        b: 25
      },
      {
        a: 12,
        b: 34
      },
      {
        a: 1,
        c: 99
      }
    ];
    const userInfo = [
      {
        z: 8
      },
      {
        z: 10
      },
    ];
    
    const cond = true;
    let getMyValue = cond ? [].concat(extraInfo, userInfo) : userInfo;
    
    console.log(getMyValue)

    0 讨论(0)
  • 2021-02-19 04:00

    Just make it

    ...(cond ? extraInfo : [])
    

    Demo with true

    var cond = true;
    
    var extraInfo = [
      {
        a: 11,
        b: 25
      },
      {
        a: 12,
        b: 34
      },
      {
        a: 1,
        c: 99
      }
    ];
    
    var userInfo = [
      {
        z: 8
      },
      {
        z: 10
      },
      ...(cond ? extraInfo : [])
    ];
    
    console.log( userInfo );

    Demo with false

    var cond = false;
    
    var extraInfo = [
      {
        a: 11,
        b: 25
      },
      {
        a: 12,
        b: 34
      },
      {
        a: 1,
        c: 99
      }
    ];
    
    var userInfo = [
      {
        z: 8
      },
      {
        z: 10
      },
      ...(cond ? extraInfo : [])
    ];
    
    console.log( userInfo );

    0 讨论(0)
  • 2021-02-19 04:09

    Another way:

    cond is true:

    var extraInfo = [
      {
        a: 11,
        b: 25
      },
      {
        a: 12,
        b: 34
      },
      {
        a: 1,
        c: 99
      }
    ]
    
    var cond = true;
    var userInfo = [
      {
        z: 8
      },
      {
        z: 10
      },
      ...(cond && extraInfo || [])
    ]
    console.log(userInfo);

    cond is false:

    var extraInfo = [
      {
        a: 11,
        b: 25
      },
      {
        a: 12,
        b: 34
      },
      {
        a: 1,
        c: 99
      }
    ]
    
    var cond = false;
    var userInfo = [
      {
        z: 8
      },
      {
        z: 10
      },
      ...(cond && extraInfo || [])
    ]
    console.log(userInfo);

    0 讨论(0)
  • 2021-02-19 04:21
    let getMyValue = cond ? [].concat(extraInfo, userInfo) : userInfo;
    

    let check this

    const extraInfo = [
      {
        a: 11,
        b: 25
      },
      {
        a: 12,
        b: 34
      },
      {
        a: 1,
        c: 99
      }
    ];
    const userInfo = [
      {
        z: 8
      },
      {
        z: 10
      },
    ];
    
    const cond = false;
    let getMyValue = cond ? [].concat(extraInfo, userInfo) : userInfo;
    
    console.log(getMyValue)

    0 讨论(0)
提交回复
热议问题