Create X amount of empty arrays based on input [duplicate] - javascript

This question already has answers here:
Declare variables programmatically?
(3 answers)
Closed 7 years ago.
A user dynamically chooses a value X. Based on the value, X, I want to create unique empty arrays. How would I go about that?
For example, user chooses 4.
I want the following output:
var array1 = [];
var array2 = [];
var array3 = [];
var array4 = [];
Is there a way to properly doing this?

You can specify the name of a property on an object using the square brackets with a string input:
var obj = {};
var numberOfArrays = 4;
for(var i = 1; i <= numberOfArrays; i++){
obj['array' + i] = []; // Specify the name of the array property
}
console.log(obj); // Logs an object which has 4 empty arrays

Just to complement Steven`s answer, you could also create an array of arrays:
var numberOfArrays = X;
var arr = [];
for(var i = 0; i < numberOfArrays; i++){
arr.push(new Array());
}

Related

App Script / Javascript filter an array with another array [duplicate]

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

Here i am trying to push the all the object in array, but its remember the last value only, pls give some suggestion [duplicate]

This question already has an answer here:
Only the last value is being repeated in an array
(1 answer)
Closed 4 years ago.
This is my code, I have pushed the data into data[], but all the data showing the same content which is the last.
var myObj = {};
var data = [];
for (var kl = 0; kl < reportCriteriaIdData.length; kl++) {
myObj["id"] = [myId];
myObj[thisobj.scFilterLabel[0]] = [reportCriteriaIdData[kl].text];
myObj["label"] = [reportCriteriaIdData[kl].text];
myObj["index"] = [kl];
data.push(myObj);
}
You need to initialize an empty object inside the for loop so that each time a new object is created and pushed in the array:
var data = [];
for(var kl = 0; kl< reportCriteriaIdData.length; kl++)
{
//initialize empty object
var myObj = {};
myObj["id"] = [myId];
myObj[thisobj.scFilterLabel[0]] = [reportCriteriaIdData[kl].text];
myObj["label"] = [reportCriteriaIdData[kl].text];
myObj["index"] = [kl];
data.push(myObj);
}

How to join each value of first array with each value of second array javascript [duplicate]

This question already has answers here:
Javascript equivalent of Python's zip function
(24 answers)
Closed 6 years ago.
Suppose we have two arrays
var arrayOne = [21.03.2016, 22.03.2016, 23.03.2016]
var arrayTwo = [23.45, 34.45, 25.76]
How can we join it so that it becomes
var joinedResultOfOneandTwo = [[21.03.2016, 23.45],
[22.03.2016, 34.45],
[23.03.2016, 25.76]]
Note It is important that we do not change the type of variables both of them should be numbers.
This is what I have tried:
for (var i = 0; i < arrayOne.length; i++) {
var clintonValues = arrayOne[i].concat(arrayTwo[i])
}
returns:
TypeError: arrayOne[i].concat is not a function
Here is an example
var arrayOne = ['21.03.2016', '22.03.2016', '23.03.2016']
var arrayTwo = ['23.45', '34.45', '25.76']
var joinedResultOfOneandTwo = [];
for (i = 0; i < arrayOne.length; i++) {
joinedResultOfOneandTwo.push([arrayOne[i], arrayTwo[i]]);
}
console.log(joinedResultOfOneandTwo);
Did you try .concat? It will join two arrays into one
var arrayOne = [21.03.2016, 22.03.2016, 23.03.2016];
var arrayTwo = [23.45, 34.45, 25.76];
arrayOne.concat(arrayTwo);//join arrayTwo into arrayOne

Count occurrences of array elements with JavaScript [duplicate]

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]

zip two arrays Javascript [duplicate]

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

Categories

Resources