1

I have two arrays in an App Script project, one with a user email and their last login date, and one with their email and license type. I'd like to combine these two arrays into an array where each item has their email, last login, and license.

It's not guaranteed that the arrays will be in the same order.

Example:

Array 1: [[google.user@domain.com, Google-Apps, 1010020020],[google.user2@domain.com, Google-Apps, 1010020020]
Array 2: [[google.user2@domain.com, 12/31/1969],[google.user@domain.com, 12/31/1969]]

becomes

Array 3: [[google.user@domain.com, 12/31/1969, Google-Apps, 1010020020],[google.user2@domain.com, 12/31/1969, Google-Apps, 1010020020]]

I tried several of the methods described in this post and this post but they seem to be ES6 and throw errors when I run them.

Ian Hyzy
  • 481
  • 5
  • 26
  • Is the first array of Array 1 always the combine partner to Array 2? Or do you need to always need to check the 'google.user@domain.com' before merging? – Jonathan Stellwag Nov 05 '18 at 21:51
  • For example, how about this? ``var Array3 = Array1.map(function(e, i){return Array2[i].concat(e)})`` – Tanaike Nov 05 '18 at 21:56
  • @JonathanStellwag No, it could be in a random order and it would need to be checked. Will clarify in the post. – Ian Hyzy Nov 05 '18 at 22:00
  • @Tanaike that didn't work unfortunately. It returned two (different) emails plus the information. – Ian Hyzy Nov 05 '18 at 22:04
  • @Ian Hyzy Thank you for replying and the additional information. I'm glad your issue was solved. – Tanaike Nov 05 '18 at 23:32

1 Answers1

1

In es5, your code should be like this:

var a1 = [['google.user@domain.com', 'Google-Apps', '1010020020'],['google.user2@domain.com', 'Google-Apps', '1010020020']]
var a2 =[['google.user@domain.com', '12/31/1969'],['google.user2@domain.com', '12/31/1969
var a3 = new Array()
for(var i=0;i<a1.length;i++){ 
    a3[i]=new Array(); 
    a3[i][0]=a1[i][0]; 
    a3[i][2]=a1[i][1]; 
    a3[i][3]=a1[i][2]; 
}
for(var i=0;i<a2.length;i++){ 
    for(var j=0;i<a3.length;j++){ 
        if(a3[j][0] == a2[i][0]){ 
            a3[j][1] = a2[i][1]; 
            break; 
        } 
    } 
}
Ramin
  • 134
  • 6
  • Would this make sure that the emails match even if they aren't in order? – Ian Hyzy Nov 05 '18 at 22:12
  • I get `TypeError: Cannot read property "0" from undefined.` on the `if(a3[j][0] == a2[i][0]){` line – Ian Hyzy Nov 05 '18 at 22:22
  • Don't forget this : var a3 = new Array(); – Ramin Nov 05 '18 at 22:23
  • I copied that line in as it appears in your example, yes. I'm pulling the data via the reports api and I think it may not be done yet. Will test this separately. – Ian Hyzy Nov 05 '18 at 22:24
  • This did work, thanks! The issue I'm having is unrelated to the question so this solves it, thanks. – Ian Hyzy Nov 05 '18 at 22:28
  • If your problem still exists, check this link: https://www.w3schools.com/code/tryit.asp?filename=FWXXZENEPUSI – Ramin Nov 05 '18 at 22:32
  • I found a case for the failure - the arrays may not be the same length either. Can a case be added to put items without a match into a seperate array? – Ian Hyzy Nov 07 '18 at 22:14
  • The answer depends on the case. You can make some changes in code to get a favorable result. Also you can use "lodash" library to write cleaner codes. Finaly, put your sample arrays here (if you want) and I will try to solve the problem. – Ramin Nov 08 '18 at 19:03