Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading