Given the scenario that I am working on a form, and I have this validation object
const errors = {
username: ['minLength', Promise<string>, Promise<string>],
password: [Promise<string>, Promise<string>]
}
// I need to convert the object to below
const errors = {
username: ['minLength', string, string],
password: [string, string]
}
The promise will all be resolved in the future so I came across this idea:
;(async function () {
for (const key in errors) {
if (errors.hasOwnProperty(key)) {
errors[key] = (await Promise.all(errors[key])).filter(Boolean)
}
}
callback(errors)
})()
Is there a more functional way to do this?
// So far I know the most elegant way to map an object
// But won't work in this scenario
const newObj = Object.fromEntries(
Object.entries(obj).map(([k, v]) => [k, v * v]),
)