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

171
Views
Joining two data sets and calculating different values

So I have an Object of two arrays that I was trying to combine.

What I am essentially aiming to do is get the unique types from dataOne. I then need the total count of occurrences for each type. Then, I need to know for each type the total count of success and failed.

Then from dataTwo I need to include the number of clicks for each type.

At the moment I have the following which achieves this, although I am sure it can be tidied up.

const ob = {
  "dataOne": [
    {
      "type": "Type 1",
      "name": "failed"
    },
    {
      "type": "Type 1",
      "name": "success"
    },
    {
      "type": "Type 2",
      "name": "success"
    },
    {
      "type": "Type 3",
      "name": "success"
    },
  ],
  "dataTwo": [
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 3",
      "name": "click",
    },
  ]
};

const dataOneReduced = ob.dataOne.reduce((acc, o) => {
  if (!acc[o.type]) {
    acc[o.type] = [
      {
        count: 0,
        success: 0,
        failed: 0,
        click: 0,
      },
    ];
  }

  acc[o.type][0]["count"] = (acc[o.type][0]["count"] || 0) + 1;
  acc[o.type][0][o.name] = acc[o.type][0][o.name] + 1;

  return acc;
}, {});

const result = ob.dataTwo.reduce((acc, o) => {
  acc[o.type][0][o.name] = acc[o.type][0][o.name] + 1;

  return acc;
}, dataOneReduced);

console.log(result);

What I am now trying to do is insert the success rate as a percentage. So I need the output to be like so

{
  "Type 1": [
    {
      "count": 2,
      "success": 1,
      "failed": 1,
      "click": 2,
      "successPecentage": 50
    }
  ],
  "Type 2": [
    {
      "count": 1,
      "success": 1,
      "failed": 0,
      "click": 2,
      "successPecentage": 100
    }
  ],
  "Type 3": [
    {
      "count": 1,
      "success": 1,
      "failed": 0,
      "click": 1,
      "successPecentage": 100,
    }
  ]
}

How would I achieve this?

Thanks

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

The desired objective may be achieved by adding one line: acc[o.type][0].successPercentage = Math.round(acc[o.type][0].success / acc[o.type][0].count * 100); as shown in below snippet

Code Snippet

const ob = {
  "dataOne": [
    {
      "type": "Type 1",
      "name": "failed"
    },
    {
      "type": "Type 1",
      "name": "success"
    },
    {
      "type": "Type 2",
      "name": "success"
    },
    {
      "type": "Type 3",
      "name": "success"
    },
  ],
  "dataTwo": [
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 3",
      "name": "click",
    },
  ]
};

const dataOneReduced = ob.dataOne.reduce((acc, o) => {
  if (!acc[o.type]) {
    acc[o.type] = [
      {
        count: 0,
        success: 0,
        failed: 0,
        click: 0,
      },
    ];
  }

  acc[o.type][0]["count"] = (acc[o.type][0]["count"] || 0) + 1;
  acc[o.type][0][o.name] = acc[o.type][0][o.name] + 1;

  return acc;
}, {});

const result = ob.dataTwo.reduce((acc, o) => {
  acc[o.type][0][o.name] = acc[o.type][0][o.name] + 1;
  acc[o.type][0].successPercentage = Math.round(acc[o.type][0].success / acc[o.type][0].count * 100);
  return acc;
}, dataOneReduced);

console.log(result);

about 4 years ago · Santiago Gelvez Report

0

Option 1

You can loop over your object one more time and insert the percentage. It is not the best solution performance-wise, but it's simpler.

const ob = {
  "dataOne": [
    {
      "type": "Type 1",
      "name": "failed"
    },
    {
      "type": "Type 1",
      "name": "success"
    },
    {
      "type": "Type 2",
      "name": "success"
    },
    {
      "type": "Type 3",
      "name": "success"
    },
  ],
  "dataTwo": [
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 3",
      "name": "click",
    },
  ]
};

const dataOneReduced = ob.dataOne.reduce((acc, o) => {
  if (!acc[o.type]) {
    acc[o.type] = [
      {
        count: 0,
        success: 0,
        failed: 0,
        click: 0,
      },
    ];
  }

  acc[o.type][0]["count"] = (acc[o.type][0]["count"] || 0) + 1;
  acc[o.type][0][o.name] = acc[o.type][0][o.name] + 1;

  return acc;
}, {});

const result = ob.dataTwo.reduce((acc, o) => {
  acc[o.type][0][o.name] = acc[o.type][0][o.name] + 1;

  return acc;
}, dataOneReduced);

for (const type of Object.keys(result)) {
  const obj = result[type][0];
  obj.successPercentage = obj.success / obj.count * 100;
}

console.log(result);

Option 2

You can insert the calculation directly into .reduce(), and it will work fine because only the value will be overwritten with each iteration, and only the last, correct value will be in the output.

const ob = {
  "dataOne": [
    {
      "type": "Type 1",
      "name": "failed"
    },
    {
      "type": "Type 1",
      "name": "success"
    },
    {
      "type": "Type 2",
      "name": "success"
    },
    {
      "type": "Type 3",
      "name": "success"
    },
  ],
  "dataTwo": [
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 2",
      "name": "click",
    },
    {
      "type": "Type 1",
      "name": "click",
    },
    {
      "type": "Type 3",
      "name": "click",
    },
  ]
};

const dataOneReduced = ob.dataOne.reduce((acc, o) => {
  if (!acc[o.type]) {
    acc[o.type] = [
      {
        count: 0,
        success: 0,
        failed: 0,
        click: 0,
      },
    ];
  }

  acc[o.type][0]["count"] = (acc[o.type][0]["count"] || 0) + 1;
  acc[o.type][0][o.name] = acc[o.type][0][o.name] + 1;
  acc[o.type][0].successPercentage = acc[o.type][0].success / acc[o.type][0].count * 100;

  return acc;
}, {});

const result = ob.dataTwo.reduce((acc, o) => {
  acc[o.type][0][o.name] = acc[o.type][0][o.name] + 1;

  return acc;
}, dataOneReduced);

console.log(result);

about 4 years ago · Santiago Gelvez 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!