Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
how do i add all js array objects to one string?

I want to use a JSON and add all of the IDs into a string, but I am having some trouble. I am fairly new to working with JSON objects, so please explain how it works if you have any ideas.

I want to convert this:

let users = [
  {"id":"24317318904217xxxx","name":"person 1"},
  {"id":"69336408842371xxxx","name":"person 2"}
]

var str = `ID\'s `

To something like this:

`ID's: <@24317318904217xxxx>, <@69336408842371xxxx>`

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use map() method in javascript to create this structure.

Basically, you create an array with use template string then push it inside your array variable and modify it.

1 - Create an array with use your data

str = users.map(user => `@${user.id}`)

2 - Add 'ID's' text into your variable and use join() method to convert your data as a string

str = `ID\s ${str.join(', ')}`

Have a nice and productive day!

Full Code :

let users = [{
    "id": "24317318904217xxxx",
    "name": "person 1"
  },
  {
    "id": "69336408842371xxxx",
    "name": "person 2"
  }
]

str = users.map(user => `<@${user.id}>`)
str = `ID\s ${str.join(', ')}`

console.log(str)

about 4 years ago · Juan Pablo Isaza Report

0

When you are new to JS, you can get started with a simple for loop over the users array like this:

let users = [
  {"id":"24317318904217xxxx","name":"person 1"},
  {"id":"69336408842371xxxx","name":"person 2"}
]

let str = "ID's: ";

for (let i = 0; i < users.length; i++) {
    str += `<@${users[i].id}>`;
    if (i < users.length - 1)
        str += ", ";
}

To access the i-th object in the array (0-indexed) use users[i];

To access an object's 'id' property simply use object.id. Same with name and all other properties.

Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

The string concatenation might look a bit weird to you, it's a string template see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

The other answer uses map and join which you can read more about on the array mdn article. I highly recommend you reading into it, as Javascript code often uses these functions, but might need so getting used to for people who only ever written iterative code.

If you have any other questions feel free to ask in the comments!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!