0
var array1 = [{issueCount: 16, failCount: 38, id: 1},
{issueCount: 15, failCount: 37, id: 2},
{issueCount: 15, failCount: 34, id: 3}];

var array2 = [{id: 1, totalAttempts: 57},
{id: 2, totalAttempts: 59},
{id: 3, totalAttempts: 67},
{id: 4, totalAttempts: 59}];

I have two arrays. From the above arrays, I need to calculate failure Percentage using the (array1. fail count/array2.totalAttempts) * 100 [id is common between two arrays]. And the final array wants in the below format.

outputArray = [{id: 1, issueCount: 16, failCount: 38, percentage: 66.66},
{id: 2, issueCount: 15, failCount: 37, percentage: 62.71},
{id: 3, issueCount: 15, failCount: 34, percentage: 50.74}];

Thanks in advance.

3
  • outputArray = []; array1.forEach(function(dataItem1, idx) { var array2Items = array2[idx]; var outputItems = {}; if(dataItem1.id == array2Items.id){ outputItems.id = dataItem1.id; outputItems.issueCount = dataItem1.issueCount; outputItems.failCount = dataItem1.failCount; outputItems.percentage = (dataItem1.failCount/array2Items.totalAttempts)*100; outputArray.push(outputItems); } }); Commented Aug 3, 2021 at 10:13
  • The above code is working fine, when I have array1 length and array2 length are same and array2 length > array1 length. But, when I have array1 length > array 2 length it is not working. what is code improvement should I need to add to achieve for all the scenario. Thanks in advance. Commented Aug 9, 2021 at 14:11
  • You should really take the tour to learn how this site works. Both solutions provided will solve your problem even with diffrent array lengths. If you just write a comment to your own answer no one will see it Commented Aug 10, 2021 at 9:06

3 Answers 3

1

You can achieve this with a simple for loop.

Just check if the id exists in the second array, if so make your calculations.

const array1 = [{issueCount: 16, failCount: 38, id: 1},
{issueCount: 15, failCount: 37, id: 2},
{issueCount: 15, failCount: 34, id: 3}];

const array2 = [{id: 1, totalAttempts: 57},
{id: 2, totalAttempts: 59},
{id: 3, totalAttempts: 67},
{id: 4, totalAttempts: 59}];

const outputArray = [];

array1.forEach(i1 => {
  const i2 = array2.find(i => i.id === i1.id);
  if(i2) {
    outputArray.push({
      id: i1.id, 
      issueCount: i1.issueCount, 
      failCount: i1.failCount,
      percentage: (i1.failCount / i2.totalAttempts) * 100
      }); 
  }
});
console.log(outputArray)

Sign up to request clarification or add additional context in comments.

Comments

1

You can do:

const array1 = [{issueCount: 16, failCount: 38, id: 1},{issueCount: 15, failCount: 37, id: 2},{issueCount: 15, failCount: 34, id: 3}]
const array2 = [{id: 1, totalAttempts: 57},{id: 2, totalAttempts: 59},{id: 3, totalAttempts: 67},{id: 4, totalAttempts: 59}]

const mergedArrays = Object.values([...array1, ...array2].reduce((a, c) => (a[c.id] = { ...a[c.id], ...c }, a), {}))
const outputArray = mergedArrays
  .filter(o => o.issueCount && o.totalAttempts)
  .map(({ id, issueCount, failCount, percentage, totalAttempts }) => ({
    id, 
    issueCount, 
    failCount,
    percentage: Math.round(failCount / totalAttempts * 100 * 100) / 100
  }))
  
console.log(outputArray)

Comments

0

Thank you all for your posts. I have also find the solution below.

outputArray = []; 
    array1.forEach(function(dataItem1, idx) { 
        var array2Items = array2[idx]; 
        var outputItems = {}; 
        if (dataItem1 && array2Items){
        if(dataItem1.id == array2Items.id){ 
            outputItems.id = dataItem1.id; 
            outputItems.issueCount = dataItem1.issueCount; 
            outputItems.failCount = dataItem1.failCount; 
            outputItems.percentage = ((dataItem1.failCount/array2Items.totalAttempts)*100).toFixed(2); 
            outputArray.push(outputItems); 
        } 
        }
    });
    
    console.log(outputArray);

2 Comments

This apporach will work with your test data but is error prone.
Hi Kevin, I agree. The real-time scenario is different than this post. In the original code, I am using find() and other company-specific proprietary tools. So, it makes us not error-prone. Thank you

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.