Firebase:Node.js:查找两个贴图对象的差异(添加或修改)
查找node.js中两个映射对象的差异的最佳方法是什么 我的地图是这样的Firebase:Node.js:查找两个贴图对象的差异(添加或修改),node.js,firebase,google-cloud-firestore,Node.js,Firebase,Google Cloud Firestore,查找node.js中两个映射对象的差异的最佳方法是什么 我的地图是这样的 roles 4K5EB8uP0G0OVWKK4YFe: "member" 4OLEnMBAwhHL4agFD1bL: "initiate" Sg0igJdOusnOaDsjDGAY: "admin" ZQH29nwMnyHqdQvRgwd0: "initiate" hqmXu2I7ehhX9NyfmfdC: &qu
roles
4K5EB8uP0G0OVWKK4YFe: "member"
4OLEnMBAwhHL4agFD1bL: "initiate"
Sg0igJdOusnOaDsjDGAY: "admin"
ZQH29nwMnyHqdQvRgwd0: "initiate"
hqmXu2I7ehhX9NyfmfdC: "initiate"
nWX51v4HxCnznkK03rne: "initiate"
会发生什么
- 可以添加新行(具有任何角色)
- 可以修改现有行(键将保持不变,但值将修改为任意值) 没有删除行的情况(至少还没有)。删除将把角色修改为“已删除”
node.js中是否有“diff”方法?您可以使用
Array.filter
查找更新后和更新前之间的差异,如下所示:
// this one keeps all the afterUpdate keys that are not in the befoteUpdate
const addedKeys = Object.keys(afterUpdate).filter(key => !beforeUpdate[key])
// this one keeps all the afterUpdate keys related to an updated value
const modifiedKeys = Object.keys(afterUpdate).filter(key => beforeUpdate[key] && beforeUpdate[key] !== afterUpdate[key])
// note this second one excludes the afterUpdate keys that are not in the beforeUpdate because they aleady are in the addedKeys
要获取值,您可以访问const addedValues=addedKeys.map(key=>afterUpdate[key])
// this one keeps all the afterUpdate keys that are not in the befoteUpdate
const addedKeys = Object.keys(afterUpdate).filter(key => !beforeUpdate[key])
// this one keeps all the afterUpdate keys related to an updated value
const modifiedKeys = Object.keys(afterUpdate).filter(key => beforeUpdate[key] && beforeUpdate[key] !== afterUpdate[key])
// note this second one excludes the afterUpdate keys that are not in the beforeUpdate because they aleady are in the addedKeys