dericcain
dericcain

Reputation: 2290

Find difference in two arrays

I have two arrays of email addresses and I am trying to find the difference in the two. One of the arrays contains current email address. The other contains current group members email address. I am updating the group list with the current email addresses and removing the address in the group that are not in the current email addresses. I cannot wrap my head around how I get my for loop to accomplish this. Here is my code so far...

for(i = 0; i < GROUP_USERS.length; i++){
    var currentMember = GROUP_USERS[i];
    for(x = 0; x < DOMAIN_USERS.length; x++){
      if(DOMAIN_USERS[x] != currentMember){
        continue;
      } else {

      }
    }

It seems like I need to test the end of my loop, or something.

EDIT

I am using Google Apps Script (SDK). I will have to push all of the emails that need to be deleted to an array and then use the GroupApps class to remove those emails from the group. Then I will need to push the DOMAIN_USERS email address that do not already reside in the group, to the group. So, essentially, I will have two arrays. One array of emails that need to be removed from the group and one array of emails that need to be added to the group. Hopefully, that makes more sense.

Upvotes: 1

Views: 189

Answers (6)

NYTom
NYTom

Reputation: 524

Its very easy using open source project jinqJs

See Fiddle Example

//Use jsJinq.com open source library
var current = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"];
var group   = ["[email protected]", "[email protected]", "[email protected]", "[email protected]"];

var currentComplex = [{email:"[email protected]"},{email: "[email protected]"}, {email:"[email protected]"}, {email:"[email protected]"}];

//Gets emails that are in current not in group
var result = jinqJs().from(current).not().in(group).select();
var result2 = jinqJs().from(currentComplex).not().in(group, 'email').select();

Upvotes: 0

Stuart
Stuart

Reputation: 9858

The 2 lists you need are the relative complement of GROUP_USERS in DOMAIN_USERS and of DOMAIN_USERS in GROUP_USERS. So define a function that finds all the members of an array a that are not in a second array b, and then use that to find which emails need to be added and deleted.

function relComp(a, b) {
    var r = [];
    for (var i = 0; i < a.length; i++) {
        if (b.indexOf(a[i]) == -1) {
            r.push(a[i]);
        }
    }
    return r;
}
var toDelete = relComp(GROUP_USERS, DOMAIN_USERS);
var toAdd = relComp(DOMAIN_USERS, GROUP_USERS);

relComp can also be written with a filter:

function relComp(a, b) {
    return a.filter(function(item) {
        return b.indexOf(item) == -1;
    });
}

JSFiddle

Upvotes: 0

ozil
ozil

Reputation: 7117

if you are looking for difference in two arrays
use grip and inarray
demo

Upvotes: 0

qk4
qk4

Reputation: 91

Unless I'm misunderstanding the logic the same result can be reached with

GROUP_USERS = DOMAIN_USERS;

Upvotes: 1

Guinn
Guinn

Reputation: 1385

If I understand correctly you want to remove all emailaddresses from GROUP_USERS that aren't in DOMAIN_USERS, then add the emailaddresses from DOMAIN_USERS that aren't in GROUP_USERS to the GROUP_USERS array, correct?

You could first make a 'contains' function (for compatability you could use this instead of indexOf()):

function contains(arr, val) {
   for (var i = 0; i < arr.length; i++) {
      if (arr[i] === val) {
        return true;
      }
   }
   return false;
}

Then to delete all emailaddresses from GROUP_USERS that aren't in DOMAIN_USERS. in a for loop:

for(i=0;i < GROUP_USERS.length; i++) {
    if(!contains(DOMAIN_USERS, GROUP_USERS[i])) {
        GROUP_USERS.splice(i, 1);
    }
}

Then another for-loop to add the ones from DOMAIN_USERS to GROUP_USERS:

for(i=0; i < DOMAIN_USERS.length; i++) {
    if(!contains(GROUP_USERS, DOMAIN_USERS[i])) {
        GROUP_USERS.push(DOMAIN_USERS[i]);
    }
}

Upvotes: 0

Anant Dabhi
Anant Dabhi

Reputation: 11104

You need create another variable to check currentMember exists in DOMAIN_USERS array after that you can remove it from GROUP_USERS array

for (i = 0; i < GROUP_USERS.length; i++) {
    var currentMember = GROUP_USERS[i];
    var isContain = false;

    for (x = 0; x < DOMAIN_USERS.length; x++) {
        if (DOMAIN_USERS[x] == currentMember) {
            isContain = true;
        }
    }
    if (!isContain) {
        emailTobeRemove.pop(currentMember);
        i--;
    }
}

Upvotes: 2

Related Questions