How to find out what was removed from an array of string?

JLeong
Mega Sage

Hi Guys,

 

I need you assistance.....

 

I have an array of names, lets call it original members  ["jo", "john", "jan" ]...

I have another array of names, lets call it new members ["jo", "john", "kevin", "joe" ] ...

 

I have 2 variables to populate as to which names were added and removed.

Based on my list,  Kevin and Joe are new members and Jan was removed. Could someone please assist as to how I can accomplish this.

 

Thank you!

 

 

1 ACCEPTED SOLUTION

Its as easy as

//You just need to revers them
//This will return "jan" in an array
var removedMembers = au.diff(origMembers, newMembers);
//This will return "kevin" and "joe" in an array
var addedMembers = au.diff(newMembers, origMembers);

View solution in original post

7 REPLIES 7

DrewW
Mega Sage
Mega Sage

In your instance there is a script include called ArrayUtil that has a diff method that you can use for this.

You just need to call it twice, once for orig-new and then new-orig and that will tell you what was removed and what was added.

 

Riya Verma
Kilo Sage
Kilo Sage

HI @JLeong 

Hope you are doing great.

 

To determine the added and removed members, you can use simple array operations. Here's a concise solution:

 

 

// Define the original and new arrays
const originalMembers = ["jo", "john", "jan"];
const newMembers = ["jo", "john", "kevin", "joe"];

// Find added members
const addedMembers = newMembers.filter(name => !originalMembers.includes(name));

// Find removed members
const removedMembers = originalMembers.filter(name => !newMembers.includes(name));

// Result
gs.info("Added members:", addedMembers);
gs.info("Removed members:", removedMembers);

 

 

 

 
 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

JLeong
Mega Sage

Thank you guys! I will try them and let you know.