0

If you want to stringify column A,B,C for a few rows it makes sense that JSON.stringify returns something like [ ["1a","2a","3a"], ["1b","2b", "3b"] ].

However if you are using just one column i.e. a 1 dimensional array, then what JSON.stringify does is terrible: [ ["1a"], ["1b"] ]

What my API expects is ["1a","1b"]

What I am missing?: How can I tell it to properly format it?

SuperWoman
  • 107
  • 1
  • 10
  • 1
    I apologize for my poor English skill. Although I'm not sure whether I could correctly understand your question, the result you expect is `console.log(JSON.stringify([["1a", "2a", "3a"], ["1b", "2b", "3b"]].map(([a]) => a)))` or `console.log(JSON.stringify([["1a"], ["1b"]].flat()))`? In this case, both results are the same like `["1a","1b"]`. – Tanaike Oct 18 '21 at 01:22
  • Related: https://stackoverflow.com/questions/63720612/what-does-the-range-method-getvalues-return-and-setvalues-accept – TheMaster Oct 18 '21 at 06:28

1 Answers1

2

From the question

However if you are using just one column i.e. a 1 dimensional array, then what JSON.stringify does is terrible: [ ["1a"], ["1b"] ]

It looks that you have a misconception, as getValues() returns a bi-dimensional no matter if the range refers to a single row or a single column. Anyway, one way to convert the bi-dimentional array into a one-dimension array is by using Array.prototype.flat().

let column = [[1],[2],[3]]
console.log(column.flat())
Rubén
  • 34,714
  • 9
  • 70
  • 166