'Not able to find the error in the approach in Google Apps Script
I import data from a google sheet into bigMatrix and then I apply filter on bigMatrix and filtered data is then assigned to a 2d array called matrix. When bigMatrix is inside the loop, everything is working perfectly. But when bigMatrix is outside the loop, then I was under the impression that I will be creating a new 2d array called matrix every time I apply filter on bigMatrix. bigMatrix is not changing anywhere in the code.
I am running a loop. I am not able to pin point the mistake.
Code 1:
var bigMatrix = mainSheet.getRange(2,1,mainSheet.getLastRow()-1,mainSheet.getLastColumn())
.getValues();
var l = bigMatrix[0].length;
for(var w=0;w<years_list.length;w++){
var purchase = [], sale = [], holdings = [], aio=[], matrix=[], matrixB=[], matrixS=[], filtered=[], writeSheet=[];
var amc = [], amcArray = [], schemes = [], schemeArray = [], folios = [], folioArray = []
var matrix = bigMatrix.filter(function(f){return f[4]<=(new Date(years_list[w],2,31))})
matrix.map(f=>f[l]=0)
This is not giving me the desired result.
Code 2:
for(var w=0;w<years_list.length;w++){
var bigMatrix = mainSheet.getRange(2,1,mainSheet.getLastRow()-1,mainSheet.getLastColumn())
.getValues();
var l = bigMatrix[0].length;
var purchase = [], sale = [], holdings = [], aio=[], matrix=[], matrixB=[], matrixS=[], filtered=[], writeSheet=[];
var amc = [], amcArray = [], schemes = [], schemeArray = [], folios = [], folioArray = []
var matrix = bigMatrix.filter(function(f){return f[4]<=(new Date(years_list[w],2,31))})
matrix.map(f=>f[l]=0)
This works fine.
As far as I know, filters create a new array. So why I have to read data from spreadsheet again and again.
Why can't I filter data from bigMatrix again and again without changing bigMatrix?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
