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

Convert an array of Object's specific key Values to Comma-Separated String

How do we Convert an Object’s specific key Values to Comma-Separated String , I can convert when it is an array but my issue is that my data is an array of objects , so I wanna convert every id to an array of strings like an example below.

#current code – just initial idea

console.log(['a', 'b', 'c'].join(','));

#example object

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

data = [
    {
        "id": 496,
    },
    {
        "id": 381,
    }
]

#expected result

  '496,381'

>Solution :

You can simply map the items of your array of objects, to extract the value of their id key:

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

data.map(item => item.id).join()
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