0

I have an array and also a string. How do I add each element of an array to the string dynamically as the array length might vary?

var array = ["123", "456"];

var str = 'Select * from abc where column_name IN (' + array [0] + ',' + array [1] + ')';

console.log(str);

1
  • 1
    Use a loop based upon the array length. Commented Feb 21, 2018 at 21:00

1 Answer 1

3

Use Array.join() on the array:

var array = ["123", "456", "789"];

var str = 'Select * from abc where column_name IN (' + array.join() + ')';

console.log(str);

You can also use a template literal instead of string concatenation:

var array = ["123", "456", "789"];

var str = `Select * from abc where column_name IN (${array.join()})`;

console.log(str);

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.