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

227
Views
How to sum the values of multiple items in a JavaScript object?

I'd like to sum the values of multiple objects.

I have an array of example 3 items, this varies and can be any number.

_items: 
0: {...}
1: {...}
2: {...}

Each of these item then has the following data:

0: 
dayOfYear: 357
dayOfWeek: 4
noOfUnits: "11"
_id: "12345"
_owner: "12345"
_createdDate: "Thu Dec 23 2021"
year: 2021
_updatedDate: "Thu Dec 23 2021"
week: 51
userId: "12345"
type: "Type 1"

I'd like to add the values of the noOfUnits variable. So for example in item 0 value is 11, item 1 is 2 and item 2 is 5. The total would become 18. This is not limited to 3 items but varies according to the number of items pulled from a database.

Thanks and happy holidays!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can reduce it

        const results = [
            {noOfUnits: "11"},
            {noOfUnits: "2"},
            {noOfUnits: "5"},
        ]
        console.log(results.reduce((acc, curr) => acc + Number(curr.noOfUnits), 0))

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

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!