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

91
Visualizações
How to display the quantity of items

The page from the database in the loop outputs elements: Dish №1, Dish №2, Dish №1, Dish №3, Dish №3

How to display their total quantity: Dish №1 - 2 items, Dish №2 - 1 items, Dish №3 - 2 items

let order = document.querySelectorAll('.order');

order.forEach(element => {
    arr = element.textContent
    console.log(arr)
});
<span class="order">Dish №1</span>
<span class="order">Dish №2</span>
<span class="order">Dish №1</span>
<span class="order">Dish №3</span>
<span class="order">Dish №3</span>

So far I have only been able to display all the items in the console, but I can't get the quantity of each item

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

0

There's couple of ways to get to the result you want, this is one of them:

let order = ['1','1','2','3','3'];
    let map = new Map();
    
    for(let i = 0; i < order.length; i++) {
        if (map.get(order[i]) === 1) {
            map.set(order[i], map.get(order[i]) + 1)
        } else {
            map.set(order[i], 1)
        }
    }

console.log(map)

So, your entries will be on map.keys() and the quantity on map.values()

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do something like this :

let order = document.querySelectorAll(".order");
let orderObj = {};
order.forEach((element) => {
  if (orderObj[element.textContent] === undefined) {
    orderObj[element.textContent] = 0;
    orderObj[element.textContent]++;
  } else {
    orderObj[element.textContent]++;
  }
});
Object.keys(orderObj).map((e) => {
  console.log(e + " - " + orderObj[e] + " items");
});
<span class="order">Dish №1</span>
<span class="order">Dish №2</span>
<span class="order">Dish №1</span>
<span class="order">Dish №3</span>
<span class="order">Dish №3</span>

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