简体   繁体   中英

How to represent JSON key identifier to JSON objects dynamically

I would like to write a script that pushes json data but in a key value pair such as

[{
   "some id" : {"some data"}
}];

so far I have been able to write this

const  allProducts= [];
const productsCollection = db.collection('products');
let productsDocument = (await productsCollection.get()) ;

productsDocument.forEach(doc => allProducts.push( { ...doc.id  [{ 
        ...doc.data() }]} ) );;

I would want something like this

productsDocument.forEach(doc => allProducts.push({ doc.id : { ...doc.data() } }));

since the identifier is the document

您可以简单地使用索引。

productsDocument.map(doc => allProducts[doc['id']] = doc.data));

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