简体   繁体   中英

How to remove multiple elements from arrays of an array using GAS?

The problem

I have a dataset encompassing empty columns. This dataset is obtained by getValues() of a defined range. Since I'll be storing this data set, I'd like to remove empty columns prior to that and, therefore, these columns need to be removed.

Dataset Sample

[
["120/1","2022-02-01T03:00:00.000Z","2022-02-01T03:00:00.000Z","Cartão Crédito","","Sicredi","","Visa","","1 + 6x","","1 de 7",100],
["120/2","2022-02-01T03:00:00.000Z","2022-03-01T03:00:00.000Z","Cartão Crédito","","Sicredi","","Visa","","1 + 6x","","2 de 7",100]
]

The evil code snippet (to me)

const colsVazias = [11, 9, 7, 5];//"Columns/elements" to be removed
    for (let i = colsVazias.length - 1; i >= 0; i--) {
      for (let r = 0; r < pagtos.length; r++) {
        pagtos[r].splice(colsVazias[i], 1)
      }
    }

I have also tried this one (borrowed from @nnnnnn), but I can't tweak it to meet the demand here. Most likely, because we're dealing with different dimensions here...

var valuesArr = ["v1","v2","v3","v4","v5"],
    removeValFromIndex = [0,2,4];    

for (var i = removeValFromIndex.length -1; i >= 0; i--)
   valuesArr.splice(removeValFromIndex[i],1);

Expected output:

[
["120/1","2022-02-01T03:00:00.000Z","2022-02-01T03:00:00.000Z","Cartão Crédito","Sicredi","Visa","1 + 6x","1 de 7",100],
["120/2","2022-02-01T03:00:00.000Z","2022-03-01T03:00:00.000Z","Cartão Crédito","Sicredi","Visa","1 + 6x","2 de 7",100]
]

Appreciate any help!

A simple map/filter combo would do it

 let pagatos = [ ["120/1","2022-02-01T03:00:00.000Z","2022-02-01T03:00:00.000Z","Cartão Crédito","","Sicredi","","Visa","","1 + 6x","","1 de 7",100], ["120/2","2022-02-01T03:00:00.000Z","2022-03-01T03:00:00.000Z","Cartão Crédito","","Sicredi","","Visa","","1 + 6x","","2 de 7",100] ]; let remove = [10, 8, 6, 4] pagatos = pagatos.map(p => p.filter((v,i) =>.remove.includes(i))) console.log(pagatos)

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