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

247
Views
Filter out and count duplicates in array, return object

I have data that looks like this

let countsRaw= [ 7, 8, 7, 6] 

I would like the output to look like this

let countsObj = {
6: {count :  1, filterState : false},
7: {count :  2, filterState : false},
8: {count :  1, filterState : false}, 
}

I have tried numerous different orientations but am not getting the output I expect from the code below, any explanation or direction would be greatly appreciated.

    let countsObj = {};
            countsRaw.forEach((x) => ({
                count: (countsObj[x] = (countsObj[x] || 0) + 1),
            }));

the result is

countObj = {6: 1, 7: 2, 8: 1}

I can't figure out why the nested objects are not being added.

Thanks (:

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

0

You just forgot to add the nested object.

const countsRaw = [ 7, 8, 7, 6];
const output = {};
countsRaw.forEach(e => {
  if (e in output) {
    output[e].count += 1;
    }
  else output[e] = { count: 1, filterState: false};
});

console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

Heres what I would do:

let duplicatesRemoved = [...new Set(array)].sort();

let countsObj = {};
    
duplicatesRemoved.forEach((v,i) => {
    countsObj[v] = {count: i, filterState: false}
});
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!