Filter the array of objects in javascript based on severity and uniqueness

Consider the below as my input array of objects:

    const arr = [ { src: 'x1', dst: 'x2', severity: null}, 
                  { src: 'x1', dst: 'x2', severity: null},
                  { src: 'x1', dst: 'x2', severity: 1},
                  { src: 'x1', dst: 'x2', severity: 2},
                  { src: 'x1', dst: 'x2', severity: 3},
                  { src: 'x1', dst: 'x2', severity: 4},
                  { src: 'x1', dst: 'x2', severity: 5},
                  { src: 'x1', dst: 'x2', severity: 6},
                  { src: 'x1', dst: 'x2', severity: 7},
                  { src: 'x2', dst: 'x1', severity: null},
                  { src: 'x2', dst: 'x1', severity: null},
                  { src: 'x2', dst: 'x1', severity: 5},
                  { src: 'x2', dst: 'x1', severity: 1},
                  { src: 'x3', dst: 'x4', severity: null}
               ];

Here src, dst and severity represents source, destination and severity of the array values. the expected result should be like below:

    [
      { src: ‘x1’, dst: ‘x2’, severity: 7 },
      { src: ‘x2’, dst: ‘x1’, severity: 5 },
      { src: ‘x3’, dst: ‘x4’, severity: null }
    ]

Basically I would have to get the array with unique src and dst, also having the highest value of severity, say here for x1 as src and x2 as destination 7 is the highest severity value, hence it is added to the resultant array, similarly for x2 as dst and x1 as src.

In case of x3 as src and x4 as dst there are no other values with x3 as src and x4 as destination hence only severity available is null, hence it is added to the array.

2 answers

  • answered 2022-05-04 10:05 temp_user

    So basically a groupBy with a little transformation to get the end result, and a max value :

    const arr = [
      { src: 'x1', dst: 'x2', severity: null },
      { src: 'x1', dst: 'x2', severity: null },
      { src: 'x1', dst: 'x2', severity: 1 },
      { src: 'x1', dst: 'x2', severity: 2 },
      { src: 'x1', dst: 'x2', severity: 3 },
      { src: 'x1', dst: 'x2', severity: 4 },
      { src: 'x1', dst: 'x2', severity: 5 },
      { src: 'x1', dst: 'x2', severity: 6 },
      { src: 'x1', dst: 'x2', severity: 7 },
      { src: 'x2', dst: 'x1', severity: null },
      { src: 'x2', dst: 'x1', severity: null },
      { src: 'x2', dst: 'x1', severity: 5 },
      { src: 'x2', dst: 'x1', severity: 1 },
      { src: 'x3', dst: 'x4', severity: null },
    ];
    
    const res = Object.entries(
      arr.reduce((p, n) => {
        const key = n.src + '.' + n.dst;
        const sev = n.severity ?? 0;
        if (!p[key]) p[key] = sev;
        else p[key] = Math.max(p[key], sev);
        return p;
      }, {}),
    ).map(([key, severity]) => ({ 
      src: key.split('.')[0], 
      dst: key.split('.')[1], 
      severity: severity || null 
    }));
    
    console.log(res);

  • answered 2022-05-04 10:06 Mihir

    const arr = [ { src: 'x1', dst: 'x2', severity: null}, 
                      { src: 'x1', dst: 'x2', severity: null},
                      { src: 'x1', dst: 'x2', severity: 1},
                      { src: 'x1', dst: 'x2', severity: 2},
                      { src: 'x1', dst: 'x2', severity: 3},
                      { src: 'x1', dst: 'x2', severity: 4},
                      { src: 'x1', dst: 'x2', severity: 5},
                      { src: 'x1', dst: 'x2', severity: 6},
                      { src: 'x1', dst: 'x2', severity: 7},
                      { src: 'x2', dst: 'x1', severity: null},
                      { src: 'x2', dst: 'x1', severity: null},
                      { src: 'x2', dst: 'x1', severity: 5},
                      { src: 'x2', dst: 'x1', severity: 1},
                      { src: 'x3', dst: 'x4', severity: null}
                   ];
    
    var helper = {};
    var result = arr.reduce(function(r, o) {
    var key = o.src + '-' + o.dst;
      
      if(!helper[key]) {
        helper[key] = Object.assign({}, o); // create a copy of o
        r.push(helper[key]);
      } else {  
        if( !helper[key].severity){
           helper[key].severity =  o.severity;
        }else{
          if(o.severity > helper[key].severity ){
            helper[key].severity = o.severity;
          }
        }
      }
      return r;
    }, []);
    
    console.log(result);

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum