简体   繁体   English

找出两个数组的差异

[英]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. 我无法解决如何让我的for循环来实现这一目标。 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). 我正在使用Google Apps脚本(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. 我将不得不将需要删除的所有电子邮件推送到阵列,然后使用GroupApps类从组中删除这些电子邮件。 Then I will need to push the DOMAIN_USERS email address that do not already reside in the group, to the group. 然后,我需要将尚未驻留在组中的DOMAIN_USERS电子邮件地址推送到该组。 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. 希望这更有意义。

You need create another variable to check currentMember exists in DOMAIN_USERS array after that you can remove it from GROUP_USERS array 您需要创建另一个变量来检查DOMAIN_USERS数组中是否存在currentMember,之后您可以从GROUP_USERS数组中删除它

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--;
    }
}

除非我误解逻辑,否则可以达到相同的结果

GROUP_USERS = DOMAIN_USERS;

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? 如果我理解正确您要删除GROUP_USERS中不在DOMAIN_USERS中的所有电子邮件地址,请将不在GROUP_USERS中的DOMAIN_USERS中的电子邮件地址添加到GROUP_USERS数组中,对吗?

You could first make a 'contains' function (for compatability you could use this instead of indexOf()): 您可以先创建一个'contains'函数(为了兼容性,您可以使用它而不是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. 然后删除GROUP_USERS中不在DOMAIN_USERS中的所有电子邮件地址。 in a for loop: 在for循环中:

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循环将DOMAIN_USERS中的那些添加到GROUP_USERS:

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

if you are looking for difference in two arrays 如果你正在寻找两个阵列的差异
use grip and inarray 使用gripinarray
demo 演示

The 2 lists you need are the relative complement of GROUP_USERS in DOMAIN_USERS and of DOMAIN_USERS in GROUP_USERS. 您需要的2个列表是DOMAIN_USERS中GROUP_USERS和GROUP_USERS中DOMAIN_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. 因此,定义一个函数,该函数查找不在第二个数组b中的数组a所有成员,然后使用该函数查找需要添加和删除的电子邮件。

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: relComp也可以用过滤器编写:

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

JSFiddle 的jsfiddle

Its very easy using open source project jinqJs 使用开源项目jinqJs非常容易

See Fiddle Example 见小提琴示例

//Use jsJinq.com open source library
var current = ["a@yahoo.com", "b@yahoo.com", "c@yahoo.com", "d@yahoo.com"];
var group   = ["a@yahoo.com", "d@yahoo.com", "yyy@yahoo.com", "xxx@yahoo.com"];

var currentComplex = [{email:"a@yahoo.com"},{email: "b@yahoo.com"}, {email:"c@yahoo.com"}, {email:"d@yahoo.com"}];

//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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM