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

135
Views
How can I calculate the length of an array of objects with reduce?

I have this simple function, and I am just trying to get the length of this array of objects. For instance I want to return 3 in that case below. I could use .length() but I want to explore more with the reduce method.

function getSum(data) {
  const totalNum = data.reduce((sum, a) => sum + a.id, 0);
  return totalNum
}

console.log(getSum([
  {id: 'ddd6929eac', isComplete: true},
  {id: 'a1dd9fbd0', isComplete: true},
  {id: 'afa8ee064', isComplete: false}
]))

Thank you very much :)

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

0

You could add one for each item in the array.

function getSum(data) {
    return data.reduce((sum, a) => sum + 1, 0);
}

const
    data = [{ id: 'ddd6929eac', isComplete: true }, { id: 'a1dd9fbd0', isComplete: true }, { id: 'afa8ee064', isComplete: false }];

console.log(getSum(data));

about 4 years ago · Juan Pablo Isaza Report

0

Well the equivalent of data.length would be:

data.reduce(sum => sum + 1, 0);

But I don't see why you would do that unless you're trying to exclude blank values.

about 4 years ago · Juan Pablo Isaza Report

0

Simply you can just add index:

const totalNum = data.reduce((sum, a, index) => index+1);
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!