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

150
Views
need to get the count of values inside the a JSON object -java script
jsondata= 
   [{"unit": "H1", "account": "ambro","domain": "DFRE"},
    {"unit": "H1","account": "ambro","domain": "DFRE"},
    {"unit": "H2","account": "Honda","domain": "HRO"},
    {"unit": "H2","account": "ford","domain": "HRO"}  ]

my output should be

{unitname : H1,no_of_accounts : 2,accounts_name:[ambro]},
{unitname : H2,no_of_accounts : 2,accounts_name:[ford,Honda]}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can easily achieve the result using Map, Set, and reduce

const jsondata = [{
    unit: "H1",
    account: "ambro",
    domain: "DFRE",
  },
  {
    unit: "H1",
    account: "ambro",
    domain: "DFRE",
  },
  {
    unit: "H2",
    account: "Honda",
    domain: "HRO",
  },
  {
    unit: "H2",
    account: "ford",
    domain: "HRO",
  },
];

const dict = jsondata.reduce((map, curr) => {
  const objInMap = map.get(curr.unit);
  if (!objInMap) {
    map.set(curr.unit, {
      unitname: curr.unit,
      no_of_accounts: 1,
      accounts_name: new Set([curr.account]),
    });
  } else {
    ++objInMap.no_of_accounts;
    objInMap.accounts_name.add(curr.account);
  }
  return map;
}, new Map());

const result = [];
for (let obj of dict.values()) {
  const o = { ...obj, accounts_name: [...obj.accounts_name] };
  result.push(o);
}

console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

const data = [
    {"unit": "H1", "account": "ambro","domain": "DFRE"},
    {"unit": "H1","account": "ambro","domain": "DFRE"},
    {"unit": "H2","account": "Honda","domain": "HRO"},
    {"unit": "H2","account": "ford","domain": "HRO"} 
];

const res = data.reduce((a, {unit, account}) => {
    a[unit] = a[unit] ?
        { ...a[unit], no_of_accounts: a[unit].no_of_accounts+1, accounts_name: [...new Set([...a[unit].accounts_name, account])] }
        :
        { unitname: unit, no_of_accounts:1, accounts_name: [account] };

    return a;
}, {});

console.log(Object.values(res));
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Here I have forEach and map method to get result you can search on google for these functions of Array.

let data = [
  {"unit": "H1", "account": "ambro","domain": "DFRE"},
  {"unit": "H1","account": "ambro","domain": "DFRE"},
  {"unit": "H2","account": "Honda","domain": "HRO"},
  {"unit": "H2","account": "ford","domain": "HRO"}
];

let result = mapData(data);
console.log(result);



// main function to get your expected result
function mapData(data) {
    let output = [];

    data.forEach(item => {
        if(output.find(a => a.unitname == item.unit)) return;

        let unitname = item.unit;
        let accounts = data.filter(a => a.unit == item.unit);
        let no_of_accounts = accounts.length;
        let accounts_name = accounts.map(a => a.account);
        accounts_name = Array.from(new Set(accounts_name)); // remove duplicate accounts name
        output.push({unitname, no_of_accounts, accounts_name});
    });

    return output;
}

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!