-3

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.

flyingfox
  • 13,414
  • 3
  • 24
  • 39
Bharat Mane
  • 55
  • 10
  • Why is that the expected result? For instance `"Application": "Google - 3618"` doesn't appear in the sample array so why should it appear like that in the result? – Rocky Sims Nov 11 '22 at 01:55
  • if we get "Application": "Google " is also fine. let me update it please – Bharat Mane Nov 11 '22 at 02:00
  • why `File Transfer Protocol - 45` is in result when it's not in `Appdata`? – Layhout Nov 11 '22 at 02:06
  • if we not matching it then it should be same as it is from `data` array – Bharat Mane Nov 11 '22 at 02:08
  • 1
    You really need to describe the relationship between the inputs (`data` and `Appdata`) and the output (`result`). Your current question is like asking "I have a list of brands and another list of clothes. I expect a list of clothing brands. Kindly help me find a solution." As you can see, there isn't enough information to know what you're even trying to do. – Rocky Sims Nov 11 '22 at 02:09
  • 1
    there are two seperate 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 – Bharat Mane Nov 11 '22 at 02:12

3 Answers3

0

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);
Rocky Sims
  • 3,523
  • 1
  • 14
  • 19
-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"];

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);
Layhout
  • 1,445
  • 1
  • 3
  • 16
-1

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);
m0squito
  • 1
  • 2