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

179
Visualizações
Targeting a <p> and converting it into an array

I would like to target all the <p> and convert it into an array. I have tried spread operator, Array.from or by using methods like .split() or .splice(). But it seems that the problem is to find the right target and understand how to operate with Element object-text which I have not yet...

const targetDates = document.querySelector('.data');
console.log(targetDates.childNodes.entries)
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <p id="dates" class="data">
            1429
    1368
    1661
    1687
    1593
    1495
    1565
    1500
    1635
        </p>
        <script src="app.js"></script>
    </body>
    </html>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

That element has a single child node, which is a text node.

const p = document.querySelector('#dates');
console.log(p.childNodes.length);
        <p id="dates" class="data">
            1429
    1368
    1661
    1687
    1593
    1495
    1565
    1500
    1635
        </p>

In such a case, it's usually easiest to use .textContent to retrieve only the text inside the element, and then you can manipulate the text to process the numbers as you like - such as splitting by newlines, or by matching numbers in the string with a regular expression:

const p = document.querySelector('#dates');
const nums = p.textContent.match(/\d+/g);
for (const num of nums)  {
  console.log(num);
}
<p id="dates" class="data">
            1429
    1368
    1661
    1687
    1593
    1495
    1565
    1500
    1635
        </p>

about 4 years ago · Juan Pablo Isaza Relatório

0

  const targetDates = document.querySelector('.data');
  const textData=targetDates.childNodes[0].textContent;
  const finalTextData=textData.trim().split('\n');//using trim() because the of the first blank space
  
  const newValue=finalTextData.map(item=>item.trim())

So with the .textcontent you can access to the content of <p> then you have to remove the blank space at first via trim() and split them base of '\n' which is Enter.Although you can find some RegExp to solve the problem ,you can also solve this issue with this steps

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