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

230
Views
How to merge string elements into one output seperated with `,`

I am trying to grab data from div elements and output them seperated with , in this way a, b How can I achieve this? For my example, text in p output should look like test, test222 dada

let desc = document.querySelectorAll(".desc");
let output = document.querySelectorAll(".output");
let items = [];

desc.forEach((item, i) => {
console.log(item.dataset.sentence);
output.textContent += item[i];
})
<div class="desc" data-sentence="test">

</div>

<div class="desc" data-sentence="test222 dada">

</div>

<p class="output">

</p>

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

0

Here's what you could try:

  1. Store your text to an array

The way you get the text with item.dataset.sentence is totally fine, you just need to put them in an array to use later.

let text = [];
desc.forEach((item, i) => {
    text.push(item.dataset.sentence);
})
  1. Convert the array to string using join()

text.join(',') => 'test,test222 dada'

  1. Update the content

document.querySelectorAll(".output") will not work as it will return an array, you will need to change it to document.querySelector(".output") to have an element.

const desc = document.querySelectorAll(".desc");
const output = document.querySelector(".output");
const text = [];

desc.forEach(item => text.push(item.dataset.sentence));
output.textContent = text.join(',');
<div class="desc" data-sentence="test"></div>
<div class="desc" data-sentence="test222 dada"></div>
<p class="output"></p>

about 4 years ago · Juan Pablo Isaza Report

0

Here's another solution demonstrating the use of advanced ECMAScript syntax features like double destructuring in the function signature, array spread ([...iterable]) along with Array.prototype.map.

let desc = document.querySelectorAll(".desc");
let output = document.querySelector(".output");
output.textContent = [...desc].map(({dataset: { sentence } }) => sentence).join(', ');
<div class="desc" data-sentence="test"></div>
<div class="desc" data-sentence="test222 dada"></div>
<p class="output"></p>

Explanations:

  1. [...desc] spreads the NodeList you have in desc into an array, which allows for the use of Array methods like map and join.
  2. ({dataset: { sentence } }) destructures the object being passed to the map iterator. The signature basically says "I expect to be passed an object that has a dataset property, which is an object that has a sentence property. Give me that sentence as a local variable inside the function body."
about 4 years ago · Juan Pablo Isaza Report

0

You almost finish it but you make some mistake.

When you select output, you only need to use querySelector instead of querySelectorAll.

let desc = document.querySelectorAll(".desc");
let output = document.querySelector(".output");
const data = [];

desc.forEach((item) => {
    data.push(item.dataset.sentence);
});

output.textContent = data.join(",");
<div class="desc" data-sentence="test">

</div>

<div class="desc" data-sentence="test222 dada">

</div>

<p class="output">

</p>

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!