Skip to content

Instantly share code, notes, and snippets.

@gabrielu
Last active June 20, 2018 20:00
Show Gist options
  • Save gabrielu/a3963820811118323c1ba384f03742a5 to your computer and use it in GitHub Desktop.
Save gabrielu/a3963820811118323c1ba384f03742a5 to your computer and use it in GitHub Desktop.
// requires babel w/ babel-preset-es2017 & babel-plugin-transform-runtime
// for Object.entries support
// .babelrc
// {
// "plugins": ["transform-runtime"],
// "presets": ["es2017"]
// }
const errors = Object.entries({
email: emailValidation.error,
password: passwordValidation.error,
}).reduce((p, [k, v]) => {
if (v) p[k] = v;
return p;
}, {});
// const errors = Object.entries({
// email: 'Required field',
// password: null
// }).reduce((p, [...kv]) => {
// if (kv[1]) p[kv[0]] = kv[1];
// return p;
// }, {});
// VV OLDER APPROACH VV
const allErrors = {
email: 'Required field',
password: null
};
const errors = Object.keys(allErrors).reduce((p, c) => {
if (allErrors[c]) p[c] = allErrors[c];
return p;
}, {});
console.log(errors); // { email: 'Required field' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment