1

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 Answer 1

2

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;}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.