Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
Get sum of values in object

I have an object that consists of n array items, each containing a name and price key. I'm attempting to retrieve, and sum the values of price in each of the entries. How do I access these values in the first place, and then sum them together?

const items = [
    {
        "name": "A",
        "price": 280000000000,
    },
    {
        "symbol": "B",
        "price": 92000000000,
    },
    {
        "symbol": "C",
        "floorPrice": 96000000000,
    }
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const items = [
    {
        "name": "A",
        "price": 280000000000,
    },
    {
        "symbol": "B",
        "price": 92000000000,
    },
    {
        "symbol": "C",
        "floorPrice": 96000000000,
    }
]
var price = 0;
items.map((item) =>  {
   if(item.hasOwnProperty("price")){price+=item.price}
   else if(item.hasOwnProperty("floorPrice")){price+=item.floorPrice}
   //... if more property 
})
console.log(price)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use the Array.reduce method available in JavaScript. Since you're objects have different keys for "price" you'd want to check which keys are present to add them to the total sum.

Reduce takes in an accumulator (running total) and current value(current spot in array. It loops through the desired values and adds them to the running total.

const sum = items.reduce((acc, curr)=>{
  if(curr.price){
    return acc + curr.price
  } else {
    return acc + curr.floorPrice
  }
 
 }, 0);

console.log(sum);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda