1

I want a simple way to compare two arrays. One array has a list of emails and the other array is a list of emails who completed a form. I want to then return a list of people who have not completed the form. Here is the function I have, but it works pretty slow.

  function findMissingUsers() {
   var sheet = getSheet();    
   users = [array of all emails]; 
   completedUsers = [array of emails who completed form];

   users.forEach(function (row) {
    completedUsers.forEach(function (user) {
     if(row.Email != user.Username) {
      console.log(row);
     }
    });
   });
  }

Just trying to find a more efficient way of doing this.

Jamie
  • 1,909
  • 3
  • 22
  • 47
  • Is the `completedUsers` array sorted or easily sortable? If so, you could try using a binary search of `completedUsers` instead of a sequential search and it should be `O(n log m)` unless I'm mistaken. – mcon Dec 16 '15 at 22:10

3 Answers3

1

How about:

completedUsers.filter(function(n) {
    return users.indexOf(n) != -1
});

from here.

Community
  • 1
  • 1
rphv
  • 5,409
  • 3
  • 29
  • 47
0

You can use e.g. lodash library and _.difference function. Details here.

scareddragon
  • 435
  • 2
  • 7
0

You can do it like this:

var incompletedUsers = users.filter(function(user){
    return completedUsers.every(function(completedUser){
        return user.Email !== completedUser.Username;
    });
});
Johan Karlsson
  • 6,419
  • 1
  • 19
  • 28