简体   繁体   English

如何使用项目列表更新 firestore 文档中的地图

[英]How to update maps in a document in firestore with a list of items

在此处输入图像描述

I have a list [a,d,f].我有一个列表 [a,d,f]。 I want to increment those fields in the test map respectively.我想分别增加测试 map 中的那些字段。

If I were to hard code it, it'll look something like this.如果我对它进行硬编码,它看起来会像这样。

firestore.collection('x').doc('y').update({
        'test.a':FieldValue.increment(1),
        'test.d':FieldValue.increment(1),
        'test.f':FieldValue.increment(1),
      }); 

But what if the list always changes.但是如果列表总是变化怎么办。 How can increment the map according to the list every time?如何每次根据列表递增map? Any suggestion would be appreciated!任何建议,将不胜感激!

You can try creating a map from that list:您可以尝试从该列表创建一个 map:

var myList = ["a", "d"];
var myMap = { for (var item in myList) 'test.$item': FieldValue.increment(1) };
print(myMap);
// {test.a: FieldValue.increment(1), test.d: FieldValue.increment(1)}

firestore.collection('x').doc('y').update(myMap)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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