1

I have an array and I want to extract a specific column's values and return the items in a comma-delimited string. What is the best way to go about this? For the below array I would like to get the values of name and return "John,Tim,Mike" Thanks for any assistance

const users = [{
  "name": "John",
  "color": "blue",
},{
  "name": "Tim",
  "color": "red",
},{
  "name": "Mike",
  "color": "green",
}]

I would like to return the results in a comma-delimited string

str = "John,Tim,Mike"

Thanks again for any assistance.

1
  • 3
    users.map(u => u.name).join(","); Commented Nov 14, 2019 at 1:30

1 Answer 1

6

You can map to extract names, then run a join:

const users = [{
  "name": "John",
  "color": "blue",
},{
  "name": "Tim",
  "color": "red",
},{
  "name": "Mike",
  "color": "green",
}];

const commaSep = users.map(item => item.name).join(', ');

console.log(commaSep);

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.