I'm refactoring my code and wanted to lessen the lines of code I've made, here's my code:
let { user, change_log, new_value, old_value } = req.query;
user = user.replace(/'/g, "\\'");
change_log = change_log.replace(/'/g, "\\'");
new_value = new_value.replace(/'/g, "\\'");
old_value = old_value.replace(/'/g, "\\'");
So I wanted to make it like single or couple of line of code like this:
user, change_log, new_value, old_value =
(user, change_log, new_value, old_value).replace(/'/g, "\\'");
So that I don't have to repeat every variable just to add a replace method. Is this possible or should I stick with the current solution?
Thanks for those who will answer and please correct me if I'm doing a bad practice in my code.
You could create an array from the values and use map
on it. Use Destructuring Assignment without declaration:
let { user, change_log, new_value, old_value } = req.query;
[user, change_log, new_value, old_value] =
[user, change_log, new_value, old_value].map(a => a.replace(/'/g, "\\'"))
Note: The previous line must have ;
at the end. Otherwise, it will consider the []
as bracket notation for req.query
. Something like this:
req.query[user, change_log, new_value, old_value]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments