Can this code be simplified to a single assignment? The three variables are inputs I receive from the frontend. I’m using the xss module in Node.js.
var clientname = xss(req.body.clientName, {
whiteList: [],
stripIgnoreTag: true,
stripIgnoreTagBody: ['script']
});
var clientnumber = xss(req.body.clientNumber, {
whiteList: [],
stripIgnoreTag: true,
stripIgnoreTagBody: ['script']
});
var clientaddress = xss(req.body.clientAddress, {
whiteList: [],
stripIgnoreTag: true,
stripIgnoreTagBody: ['script']
});
Destructuring and iteration methods are useful for this:
The only thing that changes throughout your assignments is the property name after
req.body, so pass that as an argument to an Array’smapcall, mapping each of those property names to their respectivexsscall. Once thexsscalls return, their return values are stored in an array in the same order, then destructured into three separate variables.Alternatively, you could use an object grouping them all together:
Additional considerations:
constinstead ofvar.camelCase, notalllowercase.xssin another variable for reusability and efficiency.