Home > OS >  Array of Arrays in JavaScript. Replacement for querySnapshot.forEach
Array of Arrays in JavaScript. Replacement for querySnapshot.forEach

Time:05-06

so I got this.

const querySnapshot = await getDocs(collectionRef);
querySnapshot.forEach((doc) => {
  console.log(doc.id, " => ", doc.data());
});

It outputs the following.

0  =>  Object
1 =>  Object
2  =>  Object

I want an array of arrays. Just like this.

[[0],
 [1],
 [2]]

Please help

CodePudding user response:

Well you can create and external array and push you values in there like so:

const array = [];

const querySnapshot = await getDocs(collectionRef);

querySnapshot.forEach((doc) => {
  array.push([doc.id])
});

console.log(array); // [[0],[1],[2]]

CodePudding user response:

If you reach into docs of the snapshot, you get an array and can use all array operations, such as map:

const arr = querySnapshot.docs.map((doc) => [doc.id]);
console.log(arr);
  • Related