-1

in a google apps script, I try to tranform data spread in a column into an array. I would like to know if there is any better way to code this, maybe without using a loop ?

function myFunction3() {
  var dataInCol = [['A'], ['B'], ['C'], ['D'], ['E'], ['F'], ['G']] ;

  var dataInRow = [] ;
  var i = 0 ;
  while ( i < dataInCol.length ) {
    dataInRow.push( dataInCol[i][0] ) ;
    i++ ;
  }

  // logger returns : ' [A, B, C, D, E, F, G] '
  Logger.log( dataInRow ) ;
}

Many thanks !

  • 1
    What's the problem? What's the goal? What did you try, that failed to work? – tehhowch Nov 08 '20 at 23:19
  • The aim is to search for a value with indexOf(). I got my search function to work in a column even in a line, by combining them : `.flat().flat()` It means that i search for a value in a row or a column with `Logger.log( range.indexOf(valueToSearch).flat().flat() )` in which `range` is like `[['A'], ['B'], ['C']]` or like `['A', 'B', 'C']` – Fly Jodel Nov 09 '20 at 00:21

1 Answers1

0

Use Array.prototype.flat

var arr = [['A'], ['B'], ['C'], ['D'], ['E'], ['F'], ['G']];
console.log(arr.flat())
Rubén
  • 24,097
  • 9
  • 55
  • 116