Home > Software design >  JavaScript How to group array of objects by value based on matched pattern of other array value
JavaScript How to group array of objects by value based on matched pattern of other array value

Time:11-11

I'm looking for solution - group array of objects by value based on matched pattern of other array value.

have sample array

data = [
            {
                "Application": "Chrome - 567"
            },       
            {
                "Application": "File Transfer Protocol - 45"
            },
            {
                "Application": "Google APIs - 3618"
            },
            {
                "Application": "Google Generic - 943"
            },
            {
                "Application": "Google Search - 54"
            },       
            {
                "Application": "Microsoft - 2821"
            },
            {
                "Application": "Microsoft DFSR (Distributed File System Replication) - 3722"
            },
            {
                "Application": "Microsoft Remote Procedure Call - 742"
            },              
            {
                "Application": "Telegram- 2235"
            },        
            {
                "Application": "Facebook Videos - 2250"
            },
            {
                "Application": "Facebook - 690"
            }
           ]

Other array

var Appdata = [Google, Facebook, Instagram, Microsoft, Telegram]    

expected result

result =  [
        {
            "Application": "Chrome - 567"
        },       
        {
            "Application": "File Transfer Protocol - 45"
        },
        {
            "Application": "Google"
        },    
        {
            "Application": "Microsoft"
        },                
        {
            "Application": "Telegram"
        },              
        {
            "Application": "Facebook"
        }
       ]

there are two separate array data and Appdata in data array if we match the string of Appdata array then it should replace with Appdata array value in original data array

kindly helps to find the solution for this array.

CodePudding user response:

const data = [
    {"Application": "Chrome - 567"},
    {"Application": "File Transfer Protocol - 45"},
    {"Application": "Google APIs - 3618"},
    {"Application": "Google Generic - 943"},
    {"Application": "Google Search - 54"},
    {"Application": "Microsoft - 2821"},
    {"Application": "Microsoft DFSR (Distributed File System Replication) - 3722"},
    {"Application": "Microsoft Remote Procedure Call - 742"},
    {"Application": "Telegram- 2235"},
    {"Application": "Facebook Videos - 2250"},
    {"Application": "Facebook - 690"}
];
const appData = ['Google', 'Facebook', 'Instagram', 'Microsoft', 'Telegram'];

for (let datum of data) {
    let appStr = datum.Application;
    for (let appDatum of appData) {
        if (appStr.includes(appDatum)) {
            datum.Application = appDatum;
            break;
        }
    }
}

const foundByAppStr = {};
const dataWithoutRepeats = data.filter(datum => {
    const appStr = datum.Application;
    const keep = !foundByAppStr[appStr];
    foundByAppStr[appStr] = true;
    return keep;
});

console.log(dataWithoutRepeats);

CodePudding user response:

const data = [
    {
        "Application": "Chrome - 567"
    },
    {
        "Application": "File Transfer Protocol - 45"
    },
    {
        "Application": "Google APIs - 3618"
    },
    {
        "Application": "Google Generic - 943"
    },
    {
        "Application": "Google Search - 54"
    },
    {
        "Application": "Microsoft - 2821"
    },
    {
        "Application": "Microsoft DFSR (Distributed File System Replication) - 3722"
    },
    {
        "Application": "Microsoft Remote Procedure Call - 742"
    },
    {
        "Application": "Telegram- 2235"
    },
    {
        "Application": "Facebook Videos - 2250"
    },
    {
        "Application": "Facebook - 690"
    }
]

const Appdata = ["Google", "Facebook", "Instagram", "Microsoft", "Telegram"];

const result = [...new Set(data.reduce((p, c) => {
    const found = Appdata.find(a => c.Application.includes(a));
    found !== undefined ? p.push(found) : p.push(c.Application);
    return p
}, []))].map(r => ({ Application: r }));

console.log(result);

CodePudding user response:

many ways to do this...

const data = [{
                "Application": "Chrome - 567"
            },       
            {
                "Application": "File Transfer Protocol - 45"
            },
            {
                "Application": "Google APIs - 3618"
            },
            {
                "Application": "Google Generic - 943"
            },
            {
                "Application": "Google Search - 54"
            },       
            {
                "Application": "Microsoft - 2821"
            },
            {
                "Application": "Microsoft DFSR (Distributed File System Replication) - 3722"
            },
            {
                "Application": "Microsoft Remote Procedure Call - 742"
            },              
            {
                "Application": "Telegram- 2235"
            },        
            {
                "Application": "Facebook Videos - 2250"
            },
            {
                "Application": "Facebook - 690"
            }
];
const appData = ['Google', 'Facebook', 'Instagram', 'Microsoft', 'Telegram'];

const result = data.map(dataEntry => {
    const found = appData.find(it => dataEntry.Application.includes(it))
  if(found){
    dataEntry.Application = found
    return dataEntry
  } else return dataEntry
})
const filtered = result.reduce((acc, current) => {
    if(!acc.find(data => data.Application === current.Application))
    return acc.concat([current])
  else return acc
}, [])

console.log(filtered);

CodePudding user response:

or "simply" this way...

const data = 
  [ { Application: 'Chrome - 567'                                                } 
  , { Application: 'File Transfer Protocol - 45'                                 } 
  , { Application: 'Google APIs - 3618'                                          } 
  , { Application: 'Google Generic - 943'                                        } 
  , { Application: 'Google Search - 54'                                          } 
  , { Application: 'Microsoft - 2821'                                            } 
  , { Application: 'Microsoft DFSR (Distributed File System Replication) - 3722' } 
  , { Application: 'Microsoft Remote Procedure Call - 742'                       } 
  , { Application: 'Telegram- 2235'                                              } 
  , { Application: 'Facebook Videos - 2250'                                      } 
  , { Application: 'Facebook - 690'                                              } 
  ];
const appData = [ 'Google', 'Facebook', 'Instagram', 'Microsoft', 'Telegram' ];
const res = [...data.reduce((apps,{Application}) => 
  {
  let appName = appData.find( app=>Application.includes(app))
  apps.add( appName ? appName : Application ) 
  return apps 
  }
  , new Set())].map(app=>({ Application:app}))

console.log( res )

  • Related