i'm trying to get a string from a nested object, basically it's really simple.
a server returns an error-code with a varying amount of -
-delimited strings (2-4)
now basically what i want to do is
return `${messageStrings[errorCodeParts[0]]
[errorCodeParts[1]]
[errorCodeParts[2]]
[errorCodeParts[3]]}`
but only as far down as needed... (so 2 parts, do until errorCodeParts[1]
, 3 parts, do until errorCodeParts[2]
. i don't know how to do this elegantly, can anyone point me in a direction?
this is the current code:
const messageStrings = {
auth: {
failed: 'These credentials do not match our records.',
throttle: 'Too many login attempts. Please try again in :seconds seconds.',
}
// ..
}
getString(errorCode) { // errorCode = 'auth-failed'
const errorCodeParts = Array.from(errorCode.split('-'))
let string = 'Unknown error'
return `${messageStrings[errorCodeParts[0]]
[errorCodeParts[1]]
[errorCodeParts[2]]
[errorCodeParts[3]]}` // but only those that are actually there
}