Answers for "js object conditional access property"

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

Code answers related to "js object conditional access property"

Code answers related to "Javascript"

Browse Popular Code Answers by Language