Home > Software design >  Convert an array of Object's specific key Values to Comma-Separated String
Convert an array of Object's specific key Values to Comma-Separated String

Time:09-23

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

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

#expected result

  '496,381'

CodePudding user response:

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()
  • Related