2

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'

1 Answer 1

3

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()
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.