I have an array of array of values that I make using .map():

let my data = [ [ "ted", 20 ], [ "jon", 30 ], [ "bob", 25 ] ]

And I am creating a sql statement in postgres which needs to end up with:

values (("ted", 20),("jon", 30),("bob", 25))

Is there an easy way to put this into postgres. I can change it to just text in the .map() method, but I would like to keep it as an usable array.

1 Answers

2
Shidersz On Best Solutions

You can use Array.map() and Array.join() to generate the desired string. This won't change the original data.

function toValuesStr(data)
{
    let values = data.map(([k,v]) => `("${k}", ${v})`).join(",");
    return `values (${values})`;
}

let data = [[ "ted", 20 ], [ "jon", 30 ], [ "bob", 25 ]];
console.log("String:", toValuesStr(data));
console.log("Data:", data);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}