0
[{
    "id":35,
    "aktivis":{
        "id":450,
        "name":"A. Sri Nugroho Hadi",
        "gender": "male",
        "active_job":{
            "id":491,
            "id_aktivis":450,
            "id_company":20,
            "company":{
                "id":20,
                "name":"Company 1"
            }
        }
    }
},
{
    "id":36,
    "aktivis":{
        "id":451,
        "name":"Sri ",
        "gender": "female",
        "active_job":{
            "id":492,
            "id_aktivis":451,
            "id_company":21,
            "company":{
                "id":21,
                "name":"compnay 2"
            }
        }
    }
}]

so after my previous question and there is the anwser in Group and count values in an array

so it answer my question but then i get to the another problem that i didn't realize i did have. What if i want to show my array like this?

[{
        "id":20,
        "name":"Company 1"
        "employee_count":1
        "female":0
        "male":1
    },
    {
        "id":22,
        "name":"Company 2"
        "employee_count":1
        "female":1
        "male":0
}]
1
  • What have you tried so far? Commented Apr 26, 2020 at 9:34

2 Answers 2

1
Object.values([YOUR ARRAY].reduce(function (accumulator, current) {
     const company = current.aktivis.active_job.company;
     accumulator[company.id] = accumulator[company.id] || {
       "id": company.id,
        "name": company.name,
        "employee_count": 0,
        "female": 0,
        "male": 0,
     };

     accumulator[company.id].employee_count += 1;
     if (current.aktivis.gender === 'male') {
       accumulator[company.id].male += 1;
     } else {
       accumulator[company.id].female += 1;
     }

     return accumulator;
   }, {}));
Sign up to request clarification or add additional context in comments.

Comments

0

Normally I'd create hashmap of company id to data points but since you ask for an array specifically we can also use the .findIndex method to see if a company exists.

const data = [{
    "id":35,
    "aktivis":{
        "id":450,
        "name":"A. Sri Nugroho Hadi",
        "gender": "male",
        "active_job":{
            "id":491,
            "id_aktivis":450,
            "id_company":20,
            "company":{
                "id":20,
                "name":"Company 1"
                }
            }
        }
    },
    {
    "id":36,
    "aktivis":{
        "id":451,
        "name":"Sri ",
        "gender": "female",
        "active_job":{
            "id":492,
            "id_aktivis":451,
            "id_company":21,
            "company":{
                "id":21,
                "name":"compnay 2"
                }
            }
        }
    }
]

let companies = [];

data.forEach(employee => {
    const sex = employee.aktivis.gender;
    const jobData = employee.aktivis.active_job;
    const companyIndex = companies.findIndex(c => c.id === jobData.company.id);
    const companyData = companies[companyIndex]
    if (!companyData) {
        // first time encountering this company need to create a new object
        companies.push({
            id: jobData.company.id,
            name: jobData.company.name,
            employee_count: 1,
            female: sex === "female" ? 1 : 0,
            male: sex === "male" ? 1 : 0,
        })
    } else {
        // company already exists in our array, just need to increment values
        companies[companyIndex][sex]++
        companies[companyIndex].employee_count++
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.