-1

I have two arrays.

let runArray = ['Welcome', 'Hello']

let data = [{
  Node:'Good',
  Session:'2',
  Run:'Welcome',
  Run_Group:'Display',
  Elapsed_Ms: '1000'
}]

I have like 2600 objects in data array and like 50 of them in runArray. And the number of elements could be more.

The point is to compare every index in runArray with data[index].Run propery so I can calculate the average of Elapsed_Ms property (from data).

Is there any options?

AlexZeDim
  • 3,520
  • 2
  • 28
  • 64
Heb99
  • 11
  • 4
  • Does this answer your question? [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – AlexZeDim Aug 03 '20 at 20:26
  • I noticed, that you are new member at StackOverflow! Welcome aboard. Please read [this guide](https://stackoverflow.com/help/how-to-ask) before asking, and try to search for a solution before posting a question. You might also take a look at [lodash](https://lodash.com/docs/#difference) and it's `Array.difference` methods. – AlexZeDim Aug 03 '20 at 20:29
  • No, I tried to apply that logic but that did not work because it is a guarantee that all the elements in runArray are in data array. But they are repeated more than once. That's why I just want to calculate the average of Elapsed_Ms – Heb99 Aug 03 '20 at 20:30
  • 1
    What logic have you tried? I don't see any example of your (even not working) code. I also told you, that you could you `lodash` and compare array with your array of object value, and also StackOverflow has also a lot of such question, you don't need to duplicate them, just try to search. – AlexZeDim Aug 03 '20 at 20:35
  • And yes, I looked at loadash and it's Array.difference method. But I cannot use it because I know that all the elements that are in runArray will be in Run property of data. – Heb99 Aug 03 '20 at 21:12
  • 2600 items, you would want to use reduce for that, https://codereview.stackexchange.com/questions/141530/calculate-average-of-array-of-objects-per-key-value-using-reduce – Lawrence Cherone Aug 03 '20 at 21:21
  • Why do you need to "compare two arrays" to compute the average for a "Run"? – Jake Holzinger Aug 03 '20 at 21:25
  • Because I want to calculate the average of each run @Jake Holzinger – Heb99 Aug 03 '20 at 21:47

2 Answers2

-1

Ciao, here a working example:

let runArray = ['Welcome', 'Hello']; 
    let data = [{Node:'Good', Session:'2', Run:'Welcome', Run_Group:'Display', Elapsed_Ms: '1000'},
    {Node:'Good', Session:'2', Run:'Welcome', Run_Group:'Display', Elapsed_Ms: '500'},
    {Node:'Good', Session:'2', Run:'Hello', Run_Group:'Display', Elapsed_Ms: '1000'}];

    let averages = [];
    runArray.map(el => {
       let filteredArray = data.filter(dat => dat.Run === el);
       let avg = filteredArray.reduce((a, b) => a + parseInt(b.Elapsed_Ms), 0)/filteredArray.length;
       let appo_obj = {index : el, average: avg};
       averages.push(appo_obj);
    })
    
    console.log(averages)

    

Edit: as @MattJHoughton suggested, a for loop is better for performance.

let runArray = ['Welcome', 'Hello']; 
        let data = [{Node:'Good', Session:'2', Run:'Welcome', Run_Group:'Display', Elapsed_Ms: '1000'},
        {Node:'Good', Session:'2', Run:'Welcome', Run_Group:'Display', Elapsed_Ms: '500'},
        {Node:'Good', Session:'2', Run:'Hello', Run_Group:'Display', Elapsed_Ms: '1000'}];

        let averages = [];
        for(let i=0; i<runArray.length;i++) {
           let filteredArray = data.filter(dat => dat.Run === runArray[i]);
           let avg = filteredArray.reduce((a, b) => a + parseInt(b.Elapsed_Ms), 0)/filteredArray.length;
           let appo_obj = {index : runArray[i], average: avg};
           averages.push(appo_obj);
        }
        
        console.log(averages)
Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30
  • Wrapping all this in a map function seems like it would be a bad idea for performance, as you're not intending to compute a new array from runArray, and you are building up the averages array inside the function. So you are not using the array created by map. Why not use a for loop? – MattJHoughton Aug 04 '20 at 09:40
-1

Expanding on your existing code, we need to loop on the values of your runArray finding any matching items in your data array, keeping a running sum of total elapsed time, and the data item count.

let runArray = ['Welcome', 'Hello'];

let data = [{
  Node:'Good',
  Session:'2',
  Run:'Welcome',
  Run_Group:'Display',
  Elapsed_Ms: '1000'
}];

let sumElapsedMs = 0;
let numberOfItems = 0;

// loop over runArray
runArray.forEach((value) => {

  // find the first matching entry in data array
  const matchedDataItem = data.find((dataItem) => {
    return dataItem.Run === value;
  });
  
  // sum total elapsed & number of data entries
  if (matchedDataItem) {
    numberOfItems++;
    sumElapsedMs += matchedDataItem.Elapsed_Ms;
  }
});

// calculate average (else default: undefined)
const averageElapsedMs = numberOfItems ? sumElapsedMs/numberOfItems : undefined;

console.log('Average Elapsed (mS)', averageElapsedMs);
Ananda Masri
  • 363
  • 2
  • 8