简体   繁体   中英

Array of Arrays in JavaScript. Replacement for querySnapshot.forEach

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

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]]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM