How do I return the total sum of the repeated words within all div?

Asked

Viewed 614 times

2

To illustrate the degree of difficulty, I place 3 div and within them identical words, purposeful to add their duplication.

<div>        <div>        <div>
   Diego       Ademir      Diego
   Maicon      Diego       Maicon
   Adriana     Maicon      Ademir
   Magda       Adriana     Adriana
   Delvair     Magda       Ademir
   Roselene    Delvair     Delvair
   Lawiny      Ademir      Lawiny
   Nicolas     Lawiny      Roselene
   Alice       Nicolas     Nicolas
   Ademir      Alice       Alice
   Júlia       Júlia       Ademir
</div>       </div>       </div>

I want to go through all three div with for and, returns the total number of repetition of the name "Ademir" in the alert. See an example with array:

var y = ["Diego","Maicon","Ademir","Adriana","Ademir","Delvair","Lawiny","Roselene","Nicolas","Alice","Ademir"];

var objects= {};

for (var x in y) {

objects[y[x]] = objects[y[x]]!=undefined ? objects[y[x]]+1 : 1;
}

alert("Encontramos " + objects.Ademir + " repetição do nome 'Ademir'!");

Based on the example given, I wish to make with the div and add each identical word found within the 3 div, and returns its total.

Remembering that, for the word in which it should be searched/summed is the name "Ademir".

  • You use angular in your project ?

  • @phdias No, only pure Javascript.

  • Diego, are the divs inside each other? Because if the whole content is only in the inner div of your question, or it’s like <div>João</div><div>Maria</div> ....

  • Diego, post the Divs HTML code that makes it easier to point out an assertive solution

  • @Mathias No, they’re free. Like what you mentioned <div>João</div><div>Maria</div> ....

  • @andrepaulo Observe as 3 div on the question. That’s all I have.

Show 1 more comment

1 answer

1


Simply select the whole text and count the word’s incidences in the string, take the example here.

I created a div for hold on the others, so I took the selected text of the same and counted the incidences.

//Pega texto
var str = document.getElementById("buscaraqui").textContent;
//conta o texto
var count = (str.match(/Ademir/g) || []).length;
document.getElementById("res").textContent = count
   <div id='buscaraqui'>

   <div>        <div>        <div>
  Diego       Ademir      Diego
  Maicon      Diego       Maicon
  Adriana     Maicon      Ademir
  Magda       Adriana     Adriana
  Delvair     Magda       Ademir
  Roselene    Delvair     Delvair
  Lawiny      Ademir      Lawiny
  Nicolas     Lawiny      Roselene
  Alice       Nicolas     Nicolas
  Ademir      Alice       Alice
  Júlia       Júlia       Ademir
   </div>       </div>       </div>

   </div>
   <p>O resultado é: <span id='res'></span></p>

Other References

  • I’m happy to help, this textContent is new to me, thanks, another learning.

  • Thanks for the vote. Hug

  • You’ve earned it! =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.