0

I am attempting to filter the difference between 2 arrays, I've tried to accomplish this from the following threads (and others) How to get the difference between two arrays in JavaScript? and How do I check if an array includes a value in JavaScript?

While I am able to do it with examples, I am not able to accomplish it with the actual data I am working with. I am looking to create a new array with the values that are present in "imported_id" but are not present in "old_id". In the 2 examples at the bottom of my code, the logs return the entire array of "imported_id".

With my current data, there should be 7 values in the new array (5000-5006). Is the issue based on how I'm storing/collecting the 2 arrays I'm using to filter? EDIT:

Input #1 imported_id = [['1008.0','1009.0','1010.0','1011.0','1012.0','1013.0','1014.0','1015.0','1019.0','1020.0','1022.0','1023.0','1024.0','1025.0','1027.0','1034.0','1037.0','1053.0','1054.0','1057.0','1058.0','1059.0','1060.0','1061.0','1064.0','1065.0','1068.0','1069.0','1074.0','1075.0','1076.0','1077.0','1078.0','1080.0','1081.0','1082.0','1083.0','1084.0','1085.0','1086.0','1088.0','1089.0','1091.0','1092.0','1094.0','1096.0','1097.0','1098.0','1099.0','1100.0','1102.0','1103.0','1134.0','1135.0','1136.0','1137.0','1138.0','1139.0','1140.0','1141.0','5000.0','5001.0','5002.0','5003.0','5004.0','5005.0','5006.0']]

Input #2 old_id = [['1008.0','1009.0','1010.0','1011.0','1012.0','1013.0','1014.0','1015.0','1019.0','1020.0','1022.0','1023.0','1024.0','1025.0','1027.0','1034.0','1037.0','1053.0','1054.0','1057.0','1058.0','1059.0','1060.0','1061.0','1064.0','1065.0','1068.0','1069.0','1074.0','1075.0','1076.0','1077.0','1078.0','1080.0','1081.0','1082.0','1083.0','1084.0','1085.0','1086.0','1088.0','1089.0','1091.0','1092.0','1094.0','1096.0','1097.0','1098.0','1099.0','1100.0','1102.0','1103.0','1134.0','1135.0','1136.0','1137.0','1138.0','1139.0','1140.0','1141.0']]

output = [['5000.0','5001.0','5002.0','5003.0','5004.0','5005.0','5006.0']]

Below is the log of "add_ids" and "res"

      function myfunction() {
          
          const ss =  SpreadsheetApp.getActiveSpreadsheet();
          const skillsGrid = ss.getSheetByName("Copy of Skills Grid");
          
        
          const old_id =  skillsGrid.getRange("B6:B").getValues();
          const imported_id = [];
          
            
          const courseClassification = ss.getSheetByName("Imported Course Classification").getRange("B2:X").getValues();
          
          const learningPath = skillsGrid.getRange("B3").getValue();
          const pathName = courseClassification[0].indexOf(learningPath);
          const skillID_Position = courseClassification[0].indexOf("Skill ID");
          
          
          const columns = String(skillID_Position, pathName);
          
          for (var i = 1; i < courseClassification.length; i++){
            const newData = [];
            if (courseClassification[i][pathName] >= 1){
              for (var j = 0; j < columns.length; j++) {
               newData.push(courseClassification[i][columns[j]]);
               }
               imported_id.push(newData);
            }
          }
          
          
          
          
        //Example code #1  
        const add_ids = [];
        for (var x = 0; x < imported_id.length; x++) {
          if (old_id.indexOf(imported_id[x]) == -1) {
            add_ids.push(imported_id[x]);
          }
        }
        console.log(add_ids);
          
        //Example Code #2  
        res = imported_id.filter((n) => !old_id.includes(n));
        console.log(res);



}    

  

//This works for my problem

var arr = imported_id.filter(item => !old_id.find(p => p[0] == item[0])); Logger.log(arr);

Alex
  • 37
  • 1
  • 8
  • Please provide some sample input and output. – Unmitigated Aug 06 '20 at 21:37
  • Done! Let me know if there's anything else I need to add – Alex Aug 06 '20 at 21:50
  • Your sample is wrong. That's why your previous question answer didn't work either. Can you spot the difference between your sample text and the image you're showing? – TheMaster Aug 06 '20 at 21:53
  • I adjusted the sample to include square brackets at the beginning and end of the inputs. Does this fix the sample? If so, how would this affect the way I handle the filtering? – Alex Aug 06 '20 at 22:07
  • *Does this fix the sample?* Not quite. Look more closely. In fact try typing it character by character looking at the image. – TheMaster Aug 06 '20 at 22:14
  • @Alex Does my answer work for you? – Unmitigated Aug 06 '20 at 22:20
  • @hev1 unfortunately, not with my spreadsheet data, but that is the output that I am looking for – Alex Aug 06 '20 at 22:22
  • @Alex Could you provide the data for which the method I provided does not work? – Unmitigated Aug 06 '20 at 22:23
  • @TheMaster *Changed the sample input/output again* would it be because the log is formatted as a integer/float and the input I provided earlier was a string? – Alex Aug 06 '20 at 22:24
  • No. It's a 2D array. `[[1008],[1009]]` is different from `[[1008,1009]]`. Added a couple of duplicates at top of your question. Read the links there. In any case, the first question is a exact duplicate and the filter there will work. – TheMaster Aug 06 '20 at 22:32
  • 1
    @TheMaster you've definitely earned that name. The first linked you sent worked perfectly. Lesson learned, thank you! – Alex Aug 06 '20 at 22:54

1 Answers1

1

You can use filter and includes. For better performance, you may want to use a Set.

const arr1 = ['1008','1009','1010','1011','1012','1013','1014','1015','1019','1020','1022','1023','1024','1025','1027','1034','1037','1053','1054','1057','1058','1059','1060','1061','1064','1065','1068','1069','1074','1075','1076','1077','1078','1080','1081','1082','1083','1084','1085','1086','1088','1089','1091','1092','1094','1096','1097','1098','1099','1100','1102','1103','1134','1135','1136','1137','1138','1139','1140','1141','5000','5001','5002','5003','5004','5005','5006']
const arr2 = ['1008','1009','1010','1011','1012','1013','1014','1015','1019','1020','1022','1023','1024','1025','1027','1034','1037','1053','1054','1057','1058','1059','1060','1061','1064','1065','1068','1069','1074','1075','1076','1077','1078','1080','1081','1082','1083','1084','1085','1086','1088','1089','1091','1092','1094','1096','1097','1098','1099','1100','1102','1103','1134','1135','1136','1137','1138','1139','1140','1141'];
const res = arr1.filter(x => !arr2.includes(x));
console.log(res);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80