Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda