A JSON object is returned from the event.body in a function like this:
exports.handler = async (event, context, callback) => {
{"name":"Anders","package":"Silver","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd98909c9491bd98909c9491d39e9290">[email protected]</a>","subject":"fdsafa","weightLoss":"false","strength":"on","message":"test"}
Questioning how to iterate through and detect any false values, changing them to key names instead. For example, if the 'weightLoss' value is false, a variable var weightLoss = "weightLoss"
should be created.
Pseudo code:
Map over event.body, check if any values are false, then create a new variable with the name of the key holding that false value as a string.
Additional code snippet:
exports.handler = async (event, context, callback) => {
const payload = JSON.parse(event.body)
const body1 = event.body;
Object.entries(body1).forEach(([key, value]) => {
if (value === 'false') {
body1[key] = key;
}
});
console.log(body1);
const { email, subject } = payload
...
The output will vary based on form field checks, for example:
Field checked:
{"name":"Anders","package":"Silver","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f2faf6fefbd7f2faf6fefbb9f4f8fa">[email protected]</a>","subject":"fdsafa","weightLoss":"on","strength":false,"message":"fdsasfdsdafsdafasdfasd"}
Field unchecked:
{"name":"Anders","package":"Silver","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="57323a363e3b17323a363e3b7934383a">[email protected]</a>","subject":"fdsafa","weightLoss":false,"strength":false,"message":"fdsasfdsdafsdafasdfasd"}