1

I need to build an enum from a javascript array. (I need it to populate a query)

var myArray = [113214, 432423, 65465, 65654]; //something like this
var enum = ('113214', '432423', '65465', '65654'); //into something like this

then use the enum variable as a parameter to populate my query like this:

SELECT * ... WHERE id IN (enum);

In this moment my query looks like

SELECT * ... WHERE id IN ([0934ED47E088, 0CEAC518, 3F01267E1368]);

I need the single quote for every string and no brackets.

Any ideas how to do this?

7
  • 1
    Could you add concrete usage to understand what you need? Commented Nov 7, 2016 at 13:45
  • 1
    What do you mean by "enum" in this context? Normally an "enum" has symbolic names that map to values (sometimes implicit values starting typically with 0, other times explicitly given), but your example pseudo-code doesn't seem to be doing that... Commented Nov 7, 2016 at 13:46
  • what is enums in javascript? Commented Nov 7, 2016 at 13:46
  • how does the query look like? Commented Nov 7, 2016 at 13:52
  • JS does not have enums. The closest you can do is an object that holds your enum value as key. Depending on the editor used, that might even add in autocomplete for those values. But that's about it. Commented Nov 7, 2016 at 13:53

1 Answer 1

1

You could iterate the array and build a string with single quotes around the values.

var myArray = [113214, 432423, 65465, 65654, 'abc'],
    stringified = myArray.map(function (a) {
        return '\'' + a + '\'';
    }).join(', '),
    query = 'SELECT * ... WHERE id IN (' + stringified + ');'

console.log(query);

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.