skip to Main Content

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.

3

Answers


  1. 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);
    Login or Signup to reply.
  2. 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);
    Login or Signup to reply.
  3. 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);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search