Reputation: 36267
I have an object like:
{a:1,b:2,c:3}
I'm trying to build a sqlite update statement dynamically using:
function update_json_obj_into_table(tablename, obj) {
const insertValues = Object.keys(obj).reduce((acc, k) => acc + (k + ' = ' + obj(k) + ','), "");
console.log(insertValues);
// db.run(`UPDATE ${tablename} SET ${insertValues} WHERE search_condition`);
};
based on https://www.sqlitetutorial.net/sqlite-update/ I want insertValues to look like:
' a=1,b=2,c=3 '
which I want to insert into a sqlite table.
However I'm getting the error in the title. How can I fix this?
Upvotes: 0
Views: 408
Reputation: 89314
If you want the value at that key, you should use square brackets to access the property instead.
const insertValues = Object.keys(obj).reduce((acc, k) => acc + (k + ' = ' + obj[k] + ','), "");
You can use Object.entries
as well.
const insertValues = Object.entries(obj).reduce((acc, [k,v]) => acc + (k + ' = ' + v + ','), "");
Upvotes: 2