FieldValue.arrayRemove() to remove an object from array of objects based on property value

三世轮回 提交于 2021-02-10 06:14:32

问题


I have a document with the following structure:

{
    "email" : "a@gmail.com",
    "value" : 100,
    "children" : [
                  {
                    "email" : "b@gmail.com",
                    "value" : 100
                  },
                  {
                    "email" : "b@gmail.com",
                    "value" : 200
                  }
                 ]
}

I want to remove all elements with the email b@gmail.com from the children array. I am able to remove one item if I pass the whole object to be removed like this:

FieldValue.arrayRemove(childObject)

But I want both the objects with the email b@gmail.com to be removed. Is there anyway to achieve this using FieldValue.arrayRemove()?


回答1:


The arrayRemove operation removes the exact item that you specify from the array. There is no way to pass a partial object and remove all array items that match the partial information. You will have to pass in each complete item that you want to remove.

If you don't know what those items are yet, you will typically have to first read the document, loop over the items in the array to remove them, and write the modified array back to the document.



来源:https://stackoverflow.com/questions/59694940/fieldvalue-arrayremove-to-remove-an-object-from-array-of-objects-based-on-prop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!