How to efficiently validate multiple properties

This could go in backend just aswell I realize. I will try to be a bit generic because I think the issue is not tightly coupled to my specific issue but more of a pattern that one can do. I have an object, It has a bunch of properties. I want to check if certain properties have a truthy value. What I do now is I just have a bunch of if statements that go through all the properites I want to check. And add a message to an array. I feel like there should be a better way to do it, some kind of pattern matching, or something. That's not as gross as what I am doing and is more maintainable. But not sure how to even start searching for it so I ask you guys if you have tips.
1 Reply
MarkBoots
MarkBoots8mo ago
sth like this?
const results = Object.entries(yourObject).reduce((obj, [key, value]) => {
obj[key] = Boolean(value);
return obj
},{})
const results = Object.entries(yourObject).reduce((obj, [key, value]) => {
obj[key] = Boolean(value);
return obj
},{})
It goes over every entry and returns an object with the key and if it is considered true/false
Want results from more Discord servers?
Add your server