0

I want to print something on the excel file and that excel file works on the format of array of arrays so I need to convert an array into an array of arrays like this:

['x','y','z'] to [['x'],['y'],['z']] 

I have tried to use store this using the firestore and push it on the array

const query = async() => {
  const querySnapshot = await db.collection('collectionName').get();
  const data = []
  querysnapshot.forEach(doc => {
    const x = getIdentifier(doc)
    data.push(x)
  })
  console.log(data) //it gives an array like this ['x','y','z'] 
}
query();
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 4
    `console .log (data .map (x => [x]))` should be all you need. – Scott Sauyet Mar 18 '20 at 20:47
  • 1
    Please format your code properly, it makes it a lot easier for others to read. Also, if your question is `['x','y','z'] to [['x'],['y'],['z']]` why are you adding information about firebase? – Ruan Mendes Mar 18 '20 at 20:48
  • 1
    Use any of the answers to [How to convert simple array into two-dimensional array (matrix) with Javascript](https://stackoverflow.com/q/4492385/215552) and set "number of elements per array" to 1. – Heretic Monkey Mar 18 '20 at 20:52
  • Also note that you can simplify quite a bit with `const data = querysnapshot .map (getIdentifier)` (assuming that `querysnapshot` is simply an array.) – Scott Sauyet Mar 18 '20 at 20:53
  • @ScottSauyet As true as that is, I prefer to see `querysnapshot.map((data) => getIdentifier(data))`. – Ruan Mendes Mar 18 '20 at 20:55
  • @JuanMendes: there are times when that is necessary, but as a fan of FP, I will use it only when `getIdentifier` is not unary, to coerce unary-like behavior for it. More often, I use [Ramda](https://ramdajs.com/) instead, where the [`map`](https://ramdajs.com/docs/#map) function only deals with unary callbacks, and it isn't an issue at all. – Scott Sauyet Mar 18 '20 at 20:58

1 Answers1

1

Just use map:

const array = ['x','y','z'];
const array2 = array.map(value => [value]);

console.log(array2) // [['x'],['y'],['z']] 
ZER0
  • 24,846
  • 5
  • 51
  • 54