This question already has answers here:
Copy array by value
(39 answers)
Closed 6 years ago.
I'm using JavaScript in google app script and I'm super confused why there is no simple way to copy stuff by value. I just want a simple way to copy a part of my matrix values2 into the new matrix matrix1. Why is that not possible?
var temp = [];
for (var t = 0; t<9;t++) {temp[t]= 0;}; //[0,0,0,0,0,0,0,0,0]
var matrix1 = [];
for (var x = 0; x<20;x++) {matrix1[x]=temp;};
for (var x = 0; x < 20; x++) {
for (var y = 0; y < 9 ; y++) {
matrix1[x][y] = values2[x+1][y+1];
}};
The code above fills the matrix with identical (last) lines.
Passing array as reference is common among programming languages. In this case, you don't even need temp:
var matrix1 = [];
for (var x = 0; x < 20; x++) {
matrix1[x] = [];
for (var y = 0; y < 9; y++) {
matrix1[x][y] = values2[x + 1][y + 1];
}
};
Related
This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
Closed 5 years ago.
I want to make an array so that it contains some identity name and for each of those names there is another array associated. My approach is like,
for (var i = 0; i < 10; i++) {
for (var x = 0; x < 5; x++) {
var data = someServiceCall(i, x);
var identityName = i + '-' + x;
myArray[identityName] = data;
}
}
after executing this i get something like,
[1-1: Array(8), 1-2: Array(10), 1-3: Array(10), 1-4: Array(10),.. etc]
the next time I call this function I need to check whether 1-1 exists and if yes I need to get the list related to 1-1. How can I do this..? if 1-1 is not in the myArray I will call some other function.
To check if the element having the 1-1 key exists just do:
if("1-1" in myArray)
Then to access the array associated with 1-1 use:
myArray["1-1"]
Try this. It inserts an object containing the identity name and data in each array element.
var myArray = [];
for (var i = 0; i < 10; i++) {
for (var x = 0; x < 5; x++) {
var data = someServiceCall(i, x);
var identityName = i + '-' + x;
var objectInArr = {
'identityName': identityName,
'data' : data
};
myArray.push(objectInArr);
};
};
try like this
myArray["1-1"] != undefined
Check if key exists in your array or not and act accordingly. Following is a working code snippet
var myArray = [];
for (var i = 0; i < 10; i++) {
for (var x = 0; x < 5; x++) {
var identityName = i + '-' + x;
myArray[identityName] = [1, 2];
}
}
var key = "0-0";
if(myArray[key])
console.log(myArray[key]);
you can check your array lenght and if the array is empty you will know that you need to call another action as you say. something like below might work for you
if (myArray.length === 0) {
//Call another function}
else {
//Somthing else}
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 6 years ago.
I was trying to print first array element using recursive function but output is not as expected.
var modelArray = [1,2,3];
var refurbArray = [a,b];
for (var z = 0; z < modelArray.length; z++) {
for (var y = 0; y < refurbArray.length; y++) {
var check = modelArray[z];
var recursive(refurbArray[y], function() {
consol.log(check);
});
}
}
Expected output:
1
1
2
2
3
3
Obtained output:
3
3
3
3
3
3
The problem you are having is that recursive have deferred the call to your call back function (likely due to some async functionality inside the recursive), and the value of check has changed when the callback function is finally executed.
You need to bind the check in a closure, for which there are several options and coding style on how to do, but example like
for (var z = 0; z < modelArray.length; z++) {
for (var y = 0; y < refurbArray.length; y++) {
(function() {
var check = modelArray[z];
recursive(refurbArray[y], function() {
consol.log(check);
});
})();
}
}
Try with something like:
var modelArray = [1,2,3];
var refurbArray = ['a','b'];
function recursive(val, cb){
cb();
}
for (var z = 0; z < modelArray.length; z++) {
for (var y = 0; y < refurbArray.length; y++) {
var check = modelArray[z];
recursive(refurbArray[y], function() {
console.log(check);
});
}
}
You print the check variable in the refurbCallback and that remains set to the last value of var check = modelArray[z];
This question already has answers here:
Transposing a 2D-array in JavaScript
(25 answers)
Closed 8 years ago.
If I have a large multidimensional array (matrix), how can rearange the elements so that I have "y" values as the new "x" values in the array?
Hard to explain so let me give you an example.
I want the below array.
[
[[0][1][2][3]],
[[4][5][6][7]],
[[8][9][10][11]],
[[12][13][14][15]],
]
to be transformed into the below array
[
[[0][4][8][12]],
[[1][5][9][13]],
[[2][6][10][14]],
[[3][7][11][15]],
]
for (var i = 0; i < tablesOfData.length; i++) {
for (var j = 0; j < tablesOfData[i].length; j++) {
//Transform the array
}
}
All rows needs to have the same number of columns.
If number of rows is the same as number of columns (like in your example), the following should work. If they are not the same then you need to create a n
for (var i = 0; < i tablesOfData.length; i++) {
for (var j = i; j < tablesOfData.length; j++) {
var temp = tablesOfData[i][j];
tablesOfData[i][j] = tablesOfData[j][i];
tablesOfData[j][i] = tablesOfData[i][j];
}
}
Otherwise you need to create a new table and add the values to that table, like this:
var newTable = new int[tablesOfData[i].length]();
for (var i = 0; < i tablesOfData.length; i++) {
for (var j = 0; j < tablesOfData.length; j++) {
if (i == 0)
newTable[j] = new int[tablesOfData.length]();
newTable[j][i] = tablesOfData[i][j];
}
}
I wrote the code in Notepad so it might not be the correct syntax to run on it's own but the logic should be right.
I can't get this function working. It's has to copy all the values of spreadsheet "A" to spreadsheet "B".
function triggerOnTime() {
var SpreadSheetKeyA = "142WNsfQQkSx4BuNhskDM9aXD9ylRHNZh34oO5TBTt1g";
var SpreadSheetKeyB = "1h8fDwCUUPHRdmTHu-5gMyqU294ENZxCZcHCNCuN6r_Y";
var sheet1 = SpreadsheetApp.openById(SpreadSheetKeyA).getActiveSheet();
var sheet2 = SpreadsheetApp.openById(SpreadSheetKeyB).getActiveSheet();
var data = sheet1.getDataRange().getValues();
var array = [];
for (var y = 1; y < data.length; y++) {
for (var x = 0; x < 35; x++){
array.push(data[y][x]);
}
Logger.log(array);
sheet2.appendRow(array);
sheet1.deleteRow(y);
}
}
It now copy's two or three values, but copy them multiple times (random). The function must also delete the copied values from sheetA. But it only deletes the values that are added to sheetB.
EDIT (Updated code)
function triggerOnTime() {
var SpreadSheetKeyA = "142WNsfQQkSx4BuNhskDM9aXD9ylRHNZh34oO5TBTt1g";
var SpreadSheetKeyB = "1h8fDwCUUPHRdmTHu-5gMyqU294ENZxCZcHCNCuN6r_Y";
var sheet1 = SpreadsheetApp.openById(SpreadSheetKeyA).getActiveSheet();
var sheet2 = SpreadsheetApp.openById(SpreadSheetKeyB).getActiveSheet();
var data = sheet1.getDataRange().getValues();
var array = [];
for (var y = 0; y < data.length; y++) {
for (var x = 0; x < 35; x++){
array.push(data[y][x]);
}
sheet2.appendRow(array);
sheet1.deleteRow(y+1);
array = [];
}
}
After playing around a little, I have found a fix for you (albeit a hack, I guess. Im sure there's a better way of doing it)
Whats happening is, on line 14 where you appendRow(array), you are appending the array at it's current state to the second sheet; basically creating a big pyramid of values of your array over time.
If sheet 1 contained something like this:
1,1,1
2,2,2
3,3,3
4,4,4
5,5,5
Your sheet 2 would get this appended:
1,1,1
1,1,1,2,2,2
1,1,1,2,2,2,3,3,3
... and so on.
You could do something like this:
function triggerOnTime() {
var SpreadSheetKeyA = "142WNsfQQkSx4BuNhskDM9aXD9ylRHNZh34oO5TBTt1g";
var SpreadSheetKeyB = "1h8fDwCUUPHRdmTHu-5gMyqU294ENZxCZcHCNCuN6r_Y";
var sheet1 = SpreadsheetApp.openById(SpreadSheetKeyA).getActiveSheet();
var sheet2 = SpreadsheetApp.openById(SpreadSheetKeyB).getActiveSheet();
var data = sheet1.getDataRange().getValues();
var array = [];
for (var y = 1; y < data.length; y++) {
for (var x = 0; x < 35; x++){
array.push(data[y][x]);
}
sheet2.appendRow(array);
sheet1.deleteRow(y);
array = []; //reset the array contents
}
}
The additional line will just reset the array and add the row you want to copy across.
Let me know if this works out for you.
There might be a very simple solution my problem but just not being able to find one so please help me to get to my solution in the simplest way...
The issue here is that I have data being displayed in a tabular form. Each row has 5 columns and in one of the columns it shows multiple values and so that's why I need to refer to a value by something like this row[1]['value1'], row[1]['value2'] & then row[2]['value1'], row[2]['value2'].
I declare the array
var parray = [[],[]];
I want to store the values in a loop something like this
for(counter = 0; counter < 10; counter ++){
parray[counter]['id'] += 1;
parray[counter]['isavailable'] += 0;
}
Later I want to loop through this and get the results:
for (var idx = 0; idx < parray.length; idx++) {
var pt = {};
pt.id = parray[schctr][idx].id;
pt.isavailable = parray[schctr][idx].isavailable;
}
Obviously iit's not working because Counter is a numeric key and 'id' is a string key ..my question how do I achieve this ??
Thanks for all the answers in advance.
JS has no concept of "associative arrays". You have arrays and objects (map). Arrays are objects though, and you can put keys, but it's not advisable.
You can start off with a blank array
var parray = [];
And "push" objects into it
for(counter = 0; counter < 10; counter++){
parray.push({
id : 1,
isAvailable : 0
});
}
Then you can read from them
for (var idx = 0; idx < parray.length; idx++) {
// Store the current item in a variable
var pt = parray[idx];
console.log(pt);
// read just the id
console.log(parray[idx].id);
}
Like I did here
What you want inside your array is just a plain object:
// just a regular array
var parray = [];
for(var counter = 0; counter < 10; counter++){
// create an object to store the values
var obj = {};
obj.id = counter;
obj.isavailable = 0;
// add the object to the array
parray.push(obj);
}
later:
for (var idx = 0; idx < parray.length; idx++) {
var pt = parray[idx];
// do something with pt
}