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']]
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);