简体   繁体   中英

Compare two arrays and obtain only one with data matching colum 1 in javascript/ google-apps-script

I'm not very experienced in javascript, but I've been trying to find a solution to my problem without success. I have 3 arrays with 15000 rows and 39 columns and I need to compare them to extract only values ​​that match the conditions I need in a single array.

So far I've got this code and it works but only can compare about 1000 lines at a time. There is some quicker solution that I am not finding? Already tried to look for several solutions and adpatar but I could not find a similar problem.

Any help is very welcome. Thanks in advance.

// ...definition of variables...
var newData = data3;
var novo =[]
var novo2 =[]
for(var i in data){
    var duplicate = false;
    for(var j in newData){
        //compare only if >0 to dont spend time in script
       if(data[i][33] > 0 || data[i][30] > 0 || newData[j][33] > 0 || newData[j][30] > 0) {
           //compare first column
           if(data[i].slice(0,1).toString() == newData[j].slice(0,1).toString()) {
               duplicate = true;
               var nd = newData[j].slice(30,34)
           }
       }
    }
    if(duplicate){
        novo.push(data[i].slice(0,4))
        novo2.push(nd)
    }
 }
 //set the values

Edited: Each row is with this type of data:

[3976.0, Talonete c/rebordo maf sindolor .med., 20.8, 14.61, 6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Sun Sep 18 23:00:00 GMT+00:00 2011, Wed Aug 20 23:00:00 GMT+00:00 2014, 0.0, ORTOMEDIFAR-IMPORTAÇAO E DISTRIBUIÇAO DE ARTIGOS MEDICA, Laboratórios Iberpos, SA, GH0000, 0.0]

[4286.0, PULSO ELASTICO TAM 1 BEIJE, 5.15, 3.2, 6.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Thu Oct 12 23:00:00 GMT+00:00 2017, Thu Oct 12 23:00:00 GMT+00:00 2017, 2.0, N/D, N/D, GH0000, 0.0]

[80105.0, LIMA LASER COM PEGA, 6.35, 3.65, 23.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Wed Jun 24 23:00:00 GMT+00:00 2015, Tue Jul 09 23:00:00 GMT+00:00 2013, 0.0, N/D, N/D, GH0000, 0.0]

You can save time by first creating a "hash" for your data, keyed by item.slice(0,1).toString() , and then you can find matches without having to iterate your data -- instead the hash will give you the match in constant time if there is one. With ES5 you can use a plain object to implement such a hash:

// Create the hash:
var map = {};
for (var i = 0; i < data.length; i++) {
    var item = data[i];
    map[item.slice(0,1).toString()] = item;
}

// Iterate the newData array and match with the hash: 
for (var i = 0; i < newData.length; i++) {
    var item = newData[i];
    var match = map[item.slice(0,1).toString()];
    if (match) {
        novo.push(match.slice(0,4))
        novo2.push(item.slice(30,34))
    }
}

NB: I did not include the > 0 tests to keep the solution to the essence, but you can of course still do that.

I tested only with the data you sent. But I think like this should be much faster. Please test with the full data. Ask you you didn't understand some of the code. I used only ES5 as you said you're using ES5 in Google appscript

 var data1 = [ [3976.0, 'Talonete c/rebordo maf sindolor .med.', 20.8, 14.61, 6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 777.0, 0.0, 'Sun Sep 18 23:00:00 GMT+00:00 2011', 'Wed Aug 20 23:00:00 GMT+00:00 2014', 0.0, 'ORTOMEDIFAR-IMPORTAÇAO E DISTRIBUIÇAO DE ARTIGOS MEDICA, Laboratórios Iberpos, S.A', 'GH0000', 0.0], [4286.0, 'PULSO ELASTICO TAM 1 BEIJE', 5.15, 3.2, 6.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 'Thu Oct 12 23:00:00 GMT+00:00 2017', 'Thu Oct 12 23:00:00 GMT+00:00 2017', 2.0, 'N/D', 'N/D', 'GH0000', 0.0], [80105.0, 'LIMA LASER COM PEGA', 6.35, 3.65, 23.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 555.0, 1.0, 'Wed Jun 24 23:00:00 GMT+00:00 2015', 'Tue Jul 09 23:00:00 GMT+00:00 2013', 0.0, 'N/D', 'N/D', 'GH0000', 0.0] ]; var data2 = [ [3976.0, 'Talonete c/rebordo maf sindolor .med.', 20.8, 14.61, 6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 333.0, 0.0, 'Sun Sep 18 23:00:00 GMT+00:00 2011', 'Wed Aug 20 23:00:00 GMT+00:00 2014', 0.0, 'ORTOMEDIFAR-IMPORTAÇAO E DISTRIBUIÇAO DE ARTIGOS MEDICA, Laboratórios Iberpos, S.A', 'GH0000', 0.0], [4286.0, 'PULSO ELASTICO TAM 1 BEIJE', 5.15, 3.2, 6.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 'Thu Oct 12 23:00:00 GMT+00:00 2017', 'Thu Oct 12 23:00:00 GMT+00:00 2017', 2.0, 'N/D', 'N/D', 'GH0000', 0.0], [80105.0, 'LIMA LASER COM PEGA', 6.35, 3.65, 23.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 'Wed Jun 24 23:00:00 GMT+00:00 2015', 'Tue Jul 09 23:00:00 GMT+00:00 2013', 0.0, 'N/D', 'N/D', 'GH0000', 0.0] ]; var novo =[], novo2 =[]; filter = function(e){ return ((e[30] > 0) || (e[33] > 0)); }; data1 = data1.filter(filter); data2 = data2.filter(filter); data1.map(function(row1) { var slice = row1.slice(0,1).toString(); var nd = data2 .filter(function(row2) { return slice == row2.slice(0,1).toString();}) .map(function(row2) { return row2.slice(30,34); }); if (nd) nd.map(function(rowNd) { novo.push(rowNd.slice(0,4)); novo2.push(rowNd);} ); }) console.log(novo, novo2) 

Also my experience with Google Appscript showed that the slowest thing is getting the values from and updating the spreadsheet. So try to use sheet.getRange().setValues() and .getValues() so you can get/set many values at a time instead of reading and writing cell by cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM