Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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 }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #add #property #object #conditionally
ADD COMMENT
Topic
Name
4+1 =