Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!