Answers for "conditional access an object property javascript"

38

js conditional object property

const a = {
   ...(someCondition && {b: 5})
}
Posted by: Guest on October-22-2020
0

add property to object conditionally

// Add Propperties to an object conditionally.

const isOnline = true;
const user = { 
	id: 1,
    name: 'John',
    ...(isOnline && { active: true }),
}

console.log(user);
// { id: 1, name: 'John', active: true }
Posted by: Guest on April-22-2022
0

conditionally add property to object

const trueCondition = true;const falseCondition = false;const obj = {  ...(trueCondition && { dogs: "woof" }),  ...(falseCondition && { cats: "meow" }),};// { dogs: 'woof' }
Posted by: Guest on September-13-2021

Code answers related to "conditional access an object property javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language