<script>
const obj = {
id: 1,
firstName: 'Вася',
birthday: new Date()
}
const toLocale = date => date.toLocaleDateString('ru', {year: 'numeric', month: '2-digit', day: '2-digit'})
const fields = obj => {
return Object.keys(obj).map(key => {
switch (typeof obj[key]) {
case 'string':
return `${key} = '${obj[key]}'`
case 'number':
return `${key} = ${obj[key]}`
default:
return `${key} = '${toLocale(obj[key])}'`
}
}).join(', ')
}
const str = `UPDATE users SET ${fields(obj)} WHERE id = ${obj.id}`;
console.log(str)
</script>
p.s. после сборки доработать напильником %)
|