buddha gandhi
buddha gandhi

Reputation: 19

Convert an array into an array of arrays using javascript?

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();

Upvotes: 0

Views: 104

Answers (1)

ZER0
ZER0

Reputation: 25322

Just use map:

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

console.log(array2) // [['x'],['y'],['z']] 

Upvotes: 1

Related Questions