Dynamic Object Keys
Dynamic Keys
π¨βπΌ We need to model error pages where the property name isn't a valid
identifier (like
status-404).When the property name is stored in a variable, you can use a computed property
name in an object literal:
const key = 'status-404'
const errorPages = {
[key]: '/not-found',
}
π¨ Open
and:
- Create an
errorPagesobject literal that uses the provided key variables (notFoundKey,serverErrorKey) as computed property names - Assign the matching path variables (
notFoundPath,serverErrorPath) as values β do not hard-code different key strings or paths - Export
errorPages
When finished,
errorPages should behave like:errorPages['404-status'] === '/not-found'errorPages['500-status'] === '/server-error'


