Compare individual values of 2 multi-dimensional arrays in google script -
i have 2 multi-dimensional arrays i'm trying compare data against. while length of nested arrays may vary on case-by-case basis, each instance lengths same , mirrored between both arrays , first array[0] of each contain exact same info. thing will/may vary order headers may appear in array[0]. may be
h1, h2, id
in first and
id, h1, h2
in second array.
full example
1st array
[ [ header1 , header2 , id ] , [ dataa , datab , 000 ] ]
2nd array
[ [ header1 , header2 , id ] , [ dataa , datab , 111 ] ]
question
how can parse, loop, split, whatever (still new this) can compare each values , put them single array?
result wanted
[ [ oldheader1 , oldheader2 , oldid , newheader1 , newheader2 , newid ] , [ dataa , datab , 000 , dataa , datab , 111 ] ]
and yes, dataa , datab should same both sources. i'm comparing against, ids of each things should different. if dataa doesn't match datab does, i'd continue add logging. that's later step.
i've thought looping through 1st array , taking each value, looping through 2nd value see if value exists. , can't seem figure out how .push
value , keep in order. these values being pulled sheet read faster generate array , use google's server work rather @ each cell, vlookup or find, return result, ++ , iterate.
any advice here great, thanks!
here code i'm using generate 2 arrays i'm going compare fyi. maybe there's here can modify? call function twice, file contains source info , file contains destination info.
//this take sheet , array of headers , return subset in new array function grabcolumnbyheader(headers,sheet,whichid) { var initialarray = new array(); //array store sheet data var headerindex = new array(); //blank array house header index(es) var outputarray = []; //will returned var data = sheet.getdatarange(); //all data var datanumrows = data.getnumrows(); //number of rows var datanumcol = data.getnumcolumns(); //number of columns initialarray = sheet.getrange(1, 1, datanumrows, datanumcol).getvalues(); //get index(es) of header(s). assuming headers in row 1 (i = 0;i<1;++i){ (j = 0;j<datanumcol;++j){ //loop through headers var each header (k = 0;k<headers.length;++k){ if(initialarray[i][j].tostring().touppercase() == headers[k][i].tostring().touppercase()) { headerindex.push(j); } } } } //using array's indexes headerindex, loop through , grab values //if coming source file, prepend 'source_' (i = 0;i<datanumrows;++i) { outputarray[i] = []; (j = 0;j<headerindex.length;++j) { if (i == 0 && whichid == 'true') { outputarray[i][j] = 'source_' + initialarray[i][headerindex[j]]; } else { outputarray[i][j] = initialarray[i][headerindex[j]]; } } } //logger.log(outputarray); return outputarray; }
update
here code i've been able google fu , use basic knowledge. realize performing unnecessary loops , still work in progress:
var temparray = []; var idindex; //get index of id can skipped in comparison (i=0;i<1;++i) { (j=0;j<newdata[i].length;++j) { if (newdata[i][j].tostring().touppercase() == 'id') { idindex = j; } } } //logger.log(idindex); (i=0;i<newdata.length;++i) { temparray[i] = []; //if on headers, automatically concatenate , add temparray if (i==0) { temparray[i] = newdata[i].concat(olddata[i]); } else { //logger.log('newdata['+i+']'); (j=0;j<newdata[i].length;++j) { //for newdata[i][j], begin looking in olddata //if we're not comparing indexes if (j != idindex) { //logger.log('newdata['+i+']['+j+']'); //begin looping through olddata arrays (k=0;k<olddata.length;++k){ //logger.log('newdata['+i+']['+j+'] == olddata['+k+']['+j+']'); if (newdata[i][j] == olddata[k][j]) { //need make sure here ++j same temparray[i] = newdata[i].concat(olddata[k]);//continue on j break; } } } //continue through for(j) } } //continue through for(i) } output.getrange(1, 1, temparray.length, temparray[0].length).setvalues(temparray);
found own answer after enough trial , error. may not way pro it, works me:
var temparray = []; //will used output var samearray = []; //will [sameindex,samecount] var sameindex = ''; //will index number of match used when pushing temparray var samecount = 0; //if count == keys-id push temparray //concat headers, constant temparray[0] = newdata[0].concat(olddata[0]); (i=1;i<newdata.length;++i) { //reset variables after each [i] //these used verify if newdata array , olddata array same samearray = []; sameindex = ''; samecount = 0; (j=0;j<newdata[i].length;++j) { //for newdata[i][j], begin looking in olddata //we're not comparing our ids because know they'll different //pulled out earlier , set idindex if (j != idindex) { //begin looping through olddata arrays (k=1;k<olddata.length;++k){ (l=0;l<olddata[k].length;++l) { if (l != idindex) { if (newdata[i][j] == olddata[k][l]){ samearray[0] = k; samearray[1] = ++samecount; } } } } } //since looped through of olddata array, check see //if amount of matches in single row keycount-1 //keycount variables , subtract 1 because not comaring ids if (samearray[1] == keycount-1) { temparray.push(newdata[i].concat(olddata[samearray[0]])); } else { //will used catch indexes didn't match } } } output.getrange(1, 1, temparray.length, temparray[0].length).setvalues(temparray);
Comments
Post a Comment