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

189
Visualizações
how to loop through a list of object to find total value

I have a list of objects and I need to loop through each object add all the "price" field to get the total value.

[
{
"_id":"61d167f9f5986df9dd291603",
"name":"test2",
"price":2000
},
{
"_id":"61d167f9f5986df9dd291602",
"name":"test3",
"price":4000
},
{
"_id":"61d167f9f5986df9dd291601",
"name":"test4",
"price":5000
}
]

I tried doing,

for(i in items){
let total = total + i.price;
}
console.log(total)

But dint work.

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

0

Try:

const items = [{"_id":"61d167f9f5986df9dd291603","name":"test2","price":2000},{"_id":"61d167f9f5986df9dd291602","name":"test3","price":4000},{"_id":"61d167f9f5986df9dd291601","name":"test4","price":5000}];

let total = 0;
for(let i = 0; i <= items.length - 1; i++) {
    total += items[i].price
}
console.log(total)

about 4 years ago · Juan Pablo Isaza Relatório

0

Fetch this JSOn data into the List / ArrayList and using the for each loop traverse that list using object. Example - Suppose your data is stored in list whose object name is data int total = 0 for each list_data_type in data1 total = data1.price + total;

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