notes/code/Javascript Objects

tagged: js, webdev

Conditional properties

const obj = {
...condition && { prop: value }
}

Default values

If you wanna have values with variants in an object you usually do this, which is annoying:

color: {
default: 'gray',
light: 'lightGray',
dark: 'darkGray'
}

Instead, do this:

color: Object.assign('gray', {
light: 'lightGray',
dark: 'darkGray'
})

Now you can access the default value with color instead of color.default. Thanks to Max Stoiber for this tip.