Created
September 29, 2021 13:47
-
-
Save lexeeech/d0ec041c56d6dcf047a496cf862defa0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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