How to transform array in string of new string with comma and parentheses

Issue

I have a array of values that I want to transform in a new value of strings to do a query in a external service that accept the query like this:

/v53.0/query/?q=SELECT+Id,Name,Field FROM Table WHERE Field IN ('a0C7X0000056xmxUAA', 'a0C7X0000056x9EUAQ', 'a0C7X0000056x99UAA')

example:

I have this code that transform the array:

mapIdGRD__c.map((num) => {
        return String(num);
      });

In this:


[
  'a0C7X0000056xmxUAA',
  'a0C7X0000056x9EUAQ',
  'a0C7X0000056x99UAA',
  'a0C7X0000056x8YUAQ',
  'a0C7X0000056wmxUAA',
  'a0C7X0000056wmsUAA',
  'a0C7X0000056wmnUAA',
  'a0C7X0000056wmYUAQ',
  'a0C7X0000056wmTUAQ',
  'a0C7X0000056wmOUAQ'
]

And I want to transform in a new string separated with comma and parentheses like this:

(  'a0C7X0000056xmxUAA',
  'a0C7X0000056x9EUAQ',
  'a0C7X0000056x99UAA',
  'a0C7X0000056x8YUAQ',
  'a0C7X0000056wmxUAA',
  'a0C7X0000056wmsUAA',
  'a0C7X0000056wmnUAA',
  'a0C7X0000056wmYUAQ',
  'a0C7X0000056wmTUAQ',
  'a0C7X0000056wmOUAQ'
)

Is possible using some standard lib of javascript?

Solution

you can do this

const array = [
  'a0C7X0000056xmxUAA',
  'a0C7X0000056x9EUAQ',
  'a0C7X0000056x99UAA',
  'a0C7X0000056x8YUAQ',
  'a0C7X0000056wmxUAA',
  'a0C7X0000056wmsUAA',
  'a0C7X0000056wmnUAA',
  'a0C7X0000056wmYUAQ',
  'a0C7X0000056wmTUAQ',
  'a0C7X0000056wmOUAQ'
]

const result = `(${array.map(n => `'${n}'`).join(', ')})`

console.log(result)

Answered By – R4ncid

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.