【问题标题】:Firebase & javascript - exclude items from one list to anotherFirebase 和 javascript - 将一个列表中的项目排除到另一个列表中
【发布时间】:2017-10-24 12:34:54
【问题描述】:

我脑子里一片空白。我拥有的是 2 个数组:

  1. 应用所有用户的列表
  2. 此表单的所有用户的列表

我想要做的是返回数组 1 和 2 之间的差异。

这是我的应用程序所有用户列表:

[{0: 
{id: 1234, email: test@test.com}
},{1:
{id: 1235, email: michael@test.com}
}]

那么数组 2 看起来像:

[{0: 
{id: 1234, email: test@test.com}
}]

所以我想要一个可以返回的数组(数组 1 和 2 之间的差异):

[{0:
{id: 1235, email: michael@test.com}
}]

这是我如何生成数组 1:

this.allUsersList = [];

this.sharingProvider.getUsersListRef().on('child_added', userSn => {

  if(userSn.key) {
    this.allUsersList.push({
      id: userSn.key,
      email: userSn.val().email
    });
  }

});

这是我如何生成数组 2:

this.sharingList = [];
this.sharingProvider.getFormsUsersRef(this.formId).on('child_added', fuSnap => {
  this.sharingProvider.getUsersListRef().child(fuSnap.key).on('value', userSn => {

    const myUserId = this.sharingProvider.getMyUserId();
    if(userSn.key !== myUserId) {
      this.sharingList.push({
        id: userSn.key,
        email: userSn.val().email
      });
    }
  });
});

任何帮助表示赞赏

【问题讨论】:

    标签: javascript arrays firebase firebase-realtime-database


    【解决方案1】:

    你可以使用some and Foreach ...

    let array1 = [
    {0: 
    {id: 1234, email: 'test@test.com'}
    },
    {1:
    {id: 1235, email: 'michael@test.com'}
    }
    ]
    
    let array2 = [
    {0: 
    {id: 1234, email: 'test@test.com'}
    }
    ],array3 = [];
    
    
    array1.forEach(function(e){
      let conditional = array2.some(function(element,i){ 
      return e[i] && element[i].id === e[i].id && element[i].email === e[i].email
      });
      
      if(!conditional) array3.push(e);
    });
    
    console.log(array3);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-23
      • 2021-03-17
      • 1970-01-01
      • 2017-09-28
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多