Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lexeeech/d0ec041c56d6dcf047a496cf862defa0 to your computer and use it in GitHub Desktop.
Save lexeeech/d0ec041c56d6dcf047a496cf862defa0 to your computer and use it in GitHub Desktop.
const requiredValues = ["name", "country"];
const products = [
{
key: 1,
name: "",
country: "",
},
{
key: 2,
name: "Vladimír Žosan",
country: "CZ",
},
];
const errors = products.reduce((acc, current) => {
Object.keys(current).forEach((key) => {
if (requiredValues.includes(key) && !current[key]) {
acc[current.key] = [...(acc[current.key] || []), key];
}
});
return acc;
}, {});
console.log(errors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment