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

84
Views
Accumulated values into new keyed object in Javascript

My data:

const data = [{
    "id": 1,
    "location": 30,
    "year": 2019
  },
  {
    "id": 2,
    "location": 15,
    "year": 2000
  },
  {
    "id": 1,
    "location": 25,
    "year": 2019
  }
];

My desired outcome:

accumluatedData = [{
    "id": [1],
    "year": 2019,
    "frequency": 2
  },
  {
    "id": [2],
    "year": 2000,
    "frequency": 1
  }
];

What I have so far (with the kind help of other stackoverflowers), is getting into an array (see code below).

However, I would like to get it into an object (as shown above).

Any suggestions? Am I on the right track? Or should I use a different route?

const data = [{
    "id": 1,
    "location": 30,
    "year": 2019
  },
  {
    "id": 2,
    "location": 15,
    "year": 2000
  },
  {
    "id": 1,
    "location": 25,
    "year": 2019
  }
];


var results = [];

for (var i = 0; i < data.length; ++i) {
  if (results[data[i].year]) {
    results[data[i].year]++;
  } else {
    results[data[i].year] = 1;
  }
};

console.log("results", results);

about 4 years ago · Juan Pablo Isaza
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!