[Javascript] Conditional add prop to Object

const b = true
const c = false
let object = {
  a: 1,
  ...(b ? {b: 2}: {}),
  ...(c ? {c: 3}: {})
};
console.log(object) // { a: 1, b: 2 }

 

 

posted @ 2022-03-28 13:51  Zhentiw  阅读(21)  评论(0编辑  收藏  举报