鉴于我正在处理表单的场景,并且我有这个验证对象
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]
}
承诺将在未来全部解决,所以我想到了这个想法:
;(async function () {
for (const key in errors) {
if (errors.hasOwnProperty(key)) {
errors[key] = (await Promise.all(errors[key])).filter(Boolean)
}
}
callback(errors)
})()
有没有更实用的方法来做到这一点?
// 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]),
)