This question already has answers here:
Filter a 2D Array From Elements of Another
(1 answer)
How do you resolve a "The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues" error
(2 answers)
Why does Google script for each run differently for range in a row vs range in a column
(1 answer)
If value is true in Column A, set Column C one higher
(2 answers)
Closed 2 years ago.
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);
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);
This question already has answers here:
How to count duplicate value in an array in javascript
(35 answers)
Closed 7 years ago.
I have this JavaScript array with length 129.
var fullnames = [Karri, Ismo, Grigori, Ahmed, Roope, Arto .....]
I would like to find how many times those names appeared in an array and store that information in an array like this:
var counter = [2, 5, 7, ..]
where Karri occured in fullnames array 2 times, Ismo occured 5 times etc. Any ideas about how to do it?
This is the best - and simple - way I can think of:
var fullnames = ["Karri", "Ismo", "Grigori", "Ahmed", "Roope", "Ahmed", "Karri", "Arto", "Ahmed"];
var counts = {};
for (var i = 0; i < fullnames.length; i++)
{
if (!counts.hasOwnProperty(fullnames[i]))
{
counts[fullnames[i]] = 1;
}
else
{
counts[fullnames[i]]++;
}
}
console.log(counts);
Original Fiddle.
Using an array to store the counts doesn't makes much sense, so I used an object instead.
I am assuming that fullnames is array of strings. If so, you can do it like so:
var occurences = { };
for (var i = 0; i < fullnames.length; i++) {
if (typeof occurences[fullnames[i]] == "undefined") {
occurences[fullnames[i]] = 1;
} else {
occurences[fullnames[i]]++;
}
}
console.log(occurences); // Prints out something like: {"Karri": 2, "Ismo": 5, ...}
var fullnames = ['Karri', 'Ismo', 'Grigori', 'Karri', 'Ismo', 'Grigori', 'Grigori', 'Karri', 'Ismo', 'Grigori', 'Grigori'];
var counts = [];
fullnames.forEach(function(_item) {
if(typeof counts[_item] === 'undefined') counts[_item] = 1;
else counts[_item]++;
});
var result = [];
for(i in counts) result.push(counts[i]);
console.log(result);
// outputs [3, 3, 5]
This question already has answers here:
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 9 years ago.
I have two arrays in javascript:
var array1 = ["a","b","c"];
var array2 = ["e","f","g"];
And I want the resulting array to be like this:
array3 = ["a","e","b","f","c","g"];
Any way to do this?
Will a straightforward loop do it?
array3 = new Array();
for(var i = 0; i < array1.length; i++)
{
array3.push(array1[i]);
array3.push(array2[i]);
}
You can try with concat() method:
var array1 = ["a","b","c"];
var array2 = ["e", "f","g"];
var array3 = array1.concat(array2); // Merges both arrays
For your specific requirement, you have to follow this:
function mergeArrays(a, b){
var ret = [];
for(var i = 0; i < a.length; i++){
ret.push(a[i]);
ret.push(b[i]);
}
return ret;
}
This should work:
function zip(source1, source2){
var result=[];
source1.forEach(function(o,i){
result.push(o);
result.push(source2[i]);
});
return result
}
Look http://jsfiddle.net/FGeXk/
It was not concatenation, so the answer changed.
Perhaps you would like to use: http://underscorejs.org/#zip