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

201
Vistas
sum values in an array based on another array

I am currently trying to sum some values in an array of objects, based on the Id of another array present in the current array. assuming I have this array

const queryArray = [ "1" , "2" ]

and this is the array I would like to sum if fees property values of the Id matches

const services = [
  {
    Id: "1",
    services: "Early Check-In",
    fees: "7500"
  },
  {
    Id: "2",
    services: "Late Checkout",
    fees: "7500"
  },
  {
    Id: "3",
    services: "Airport Chauffeur",
    fees: "25000"
  }
]

so I will like to get the sum of the fees property value in the services array up here based on the Id available in the queryArray. so in this case

[ "1" , "2" ]

should return

15000

if I decided to use this below

[ "1" , "3" ]

it should return

32500

and if also do

[ "1", "2" , "3"]

it should return

40000

Am new to javascript so I can't even think of any possible solution or the step to take at all.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Sure, filter the services you need -> map service to it's value -> reduce the resulting list to a singe number adding the elements:

const services = [
  {
    Id: "1",
    services: "Early Check-In",
    fees: "7500"
  },
  {
    Id: "2",
    services: "Late Checkout",
    fees: "7500"
  },
  {
    Id: "3",
    services: "Airport Chauffeur",
    fees: "25000"
  }
]
const ids = ["1" , "2"]

console.log(
  services
    .filter(({Id}) => ids.includes(Id))
    .map(({fees}) => parseFloat(fees))
    .reduce((acc, fees) => acc + fees, 0)
  
)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Check whether the id is included in the id array inside the reducer:

const services = [
  {
    Id: "1",
    services: "Early Check-In",
    fees: "7500"
  },
  {
    Id: "2",
    services: "Late Checkout",
    fees: "7500"
  },
  {
    Id: "3",
    services: "Airport Chauffeur",
    fees: "25000"
  }
]

const queryArray = [ "1" , "2" ]

const res = services.reduce((a,b) => queryArray.includes(b.Id) ? a += +b.fees : a, 0)

console.log(res)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could also use reduce, and for every entry check if the Id of the current value is in the queryArray. If it is, then return the current total plus the current fees.

Set a 0 as the start value.

const services = [{
    Id: "1",
    services: "Early Check-In",
    fees: "7500"
  },
  {
    Id: "2",
    services: "Late Checkout",
    fees: "7500"
  },
  {
    Id: "3",
    services: "Airport Chauffeur",
    fees: "25000"
  }
]

const queryArray = ["1", "2"]

let res = services.reduce(
  (total, curr) => queryArray.includes(curr.Id) ? total + parseInt(curr.fees, 10) : total,
  0
);
console.log(res);

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