Home > Software engineering >  sum values in an array based on another array
sum values in an array based on another array

Time:10-16

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.

CodePudding user response:

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)
  
)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related