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();
6
  • 4
    console .log (data .map (x => [x])) should be all you need. Commented Mar 18, 2020 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? Commented Mar 18, 2020 at 20:48
  • 1
    Use any of the answers to How to convert simple array into two-dimensional array (matrix) with Javascript and set "number of elements per array" to 1. Commented Mar 18, 2020 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.) Commented Mar 18, 2020 at 20:53
  • @ScottSauyet As true as that is, I prefer to see querysnapshot.map((data) => getIdentifier(data)). Commented Mar 18, 2020 at 20:55

1 Answer 1

1

Just use map:

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

console.log(array2) // [['x'],['y'],['z']] 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.