使用字符串数组过滤对象数组

时间:2020-07-28 00:14:18

标签: javascript

我想通过字符串数组过滤掉对象数组

let allUsers = [
{
  "avatar":null,
  "deleted":false,
  "_id":"5f1f5df967b3240004f50a36",
  "email":"emailroom3@test.com",
  "password":"$2a$10$5eKv6OVouPnPH.3g9STEYO990V.L14kpD2ImZ3AtZtHaRjCg4oj/6",
  "fcm":"fcm3",
  "createdAt":"2020-07-27T23:06:33.683Z",
  "updatedAt":"2020-07-27T23:06:33.683Z",
  "__v":0
},
{
  "avatar":null,
  "deleted":false,
  "_id":"5f1f5e0067b3240004f50a37",
  "email":"emailroom2@test.com",
  "password":"$2a$10$czA2dASdEo1Dw0oAxw.y1eJwg4lrZW12tLE6sUeG5k.PThuineTIm",
  "fcm":"fcm2",
  "createdAt":"2020-07-27T23:06:40.702Z",
  "updatedAt":"2020-07-27T23:06:40.702Z","__v":0
},
{
  "avatar":null,
  "deleted":false,
  "_id":"5f1f5d9167b3240004f50a33",
  "email":"emailroom1@test.com",
  "password":"$2a$10$0O2aDCCSibWlNznIWfHTxuY8vNq4Sge/YDRuntKJl9UKnSI.9TEfC",
  "fcm":"fcm1",
  "createdAt":"2020-07-27T23:04:49.285Z",
  "updatedAt":"2020-07-27T23:04:49.285Z",
  "__v":0
}]

let connectedUsers = ['5f1f5df967b3240004f50a36','5f1f5e0067b3240004f50a37']

输出应该是不在第二个数组中的第三个用户:

myArrayFiltered = [
  {
    "avatar":null,
    "deleted":false,
    "_id":"5f1f5d9167b3240004f50a33",
    "email":"emailroom1@test.com",
    "password":"$2a$10$0O2aDCCSibWlNznIWfHTxuY8vNq4Sge/YDRuntKJl9UKnSI.9TEfC",
    "fcm":"fcm1",
    "createdAt":"2020-07-27T23:04:49.285Z",
    "updatedAt":"2020-07-27T23:04:49.285Z",
    "__v":0
  }
]

我尝试过

 let myArrayFiltered = allUsers.filter((user) => {
    return connectedUsers.some((el) => {
      return user._id !== el;
    });
  });

但它会返回所有3个用户

修改: 所有答案都返回第二个数组中的2个用户,但是我想要的不是,而是ID为'5f1f5d9167b3240004f50a33'的用户

3 个答案:

答案 0 :(得分:1)

您不需要遍历第二个数组。您只需要检查user._id是否在connectedUsers数组中。像这样:

let allUsers = [{"avatar":null,"deleted":false,"_id":"5f1f5df967b3240004f50a36","email":"emailroom3@test.com","password":"$2a$10$5eKv6OVouPnPH.3g9STEYO990V.L14kpD2ImZ3AtZtHaRjCg4oj/6","fcm":"fcm3","createdAt":"2020-07-27T23:06:33.683Z","updatedAt":"2020-07-27T23:06:33.683Z","__v":0},{"avatar":null,"deleted":false,"_id":"5f1f5e0067b3240004f50a37","email":"emailroom2@test.com","password":"$2a$10$czA2dASdEo1Dw0oAxw.y1eJwg4lrZW12tLE6sUeG5k.PThuineTIm","fcm":"fcm2","createdAt":"2020-07-27T23:06:40.702Z","updatedAt":"2020-07-27T23:06:40.702Z","__v":0},{"avatar":null,"deleted":false,"_id":"5f1f5d9167b3240004f50a33","email":"emailroom1@test.com","password":"$2a$10$0O2aDCCSibWlNznIWfHTxuY8vNq4Sge/YDRuntKJl9UKnSI.9TEfC","fcm":"fcm1","createdAt":"2020-07-27T23:04:49.285Z","updatedAt":"2020-07-27T23:04:49.285Z","__v":0}];
let connectedUsers = ['5f1f5df967b3240004f50a36','5f1f5e0067b3240004f50a37'];
let myArrayFiltered = allUsers.filter(user => {
  return connectedUsers.includes(user._id);
});
console.log(myArrayFiltered);
  
// myArrayFiltered = [{"avatar":null,"deleted":false,"_id":"5f1f5d9167b3240004f50a33","email":"emailroom1@test.com","password":"$2a$10$0O2aDCCSibWlNznIWfHTxuY8vNq4Sge/YDRuntKJl9UKnSI.9TEfC","fcm":"fcm1","createdAt":"2020-07-27T23:04:49.285Z","updatedAt":"2020-07-27T23:04:49.285Z","__v":0}]

答案 1 :(得分:1)

您可以使用indexOf方法检查数组中的项是否为

let myArrayFiltered = allUsers.filter((user) => {
        return connectedUsers.indexOf(user._id) > -1;
      });

let allUsers = 
[{"avatar":null,"deleted":false,"_id":"5f1f5df967b3240004f50a36","email":"emailroom3@test.com","password":"$2a$10$5eKv6OVouPnPH.3g9STEYO990V.L14kpD2ImZ3AtZtHaRjCg4oj/6","fcm":"fcm3","createdAt":"2020-07-27T23:06:33.683Z","updatedAt":"2020-07-27T23:06:33.683Z","__v":0},{"avatar":null,"deleted":false,"_id":"5f1f5e0067b3240004f50a37","email":"emailroom2@test.com","password":"$2a$10$czA2dASdEo1Dw0oAxw.y1eJwg4lrZW12tLE6sUeG5k.PThuineTIm","fcm":"fcm2","createdAt":"2020-07-27T23:06:40.702Z","updatedAt":"2020-07-27T23:06:40.702Z","__v":0},{"avatar":null,"deleted":false,"_id":"5f1f5d9167b3240004f50a33","email":"emailroom1@test.com","password":"$2a$10$0O2aDCCSibWlNznIWfHTxuY8vNq4Sge/YDRuntKJl9UKnSI.9TEfC","fcm":"fcm1","createdAt":"2020-07-27T23:04:49.285Z","updatedAt":"2020-07-27T23:04:49.285Z","__v":0}]
 let connectedUsers = ['5f1f5df967b3240004f50a36','5f1f5e0067b3240004f50a37']
 
  let myArrayFiltered = allUsers.filter((user) => {
    return connectedUsers.indexOf(user._id) > -1;
  });
  
  console.log(myArrayFiltered)

答案 2 :(得分:0)

在您的解决方案中,您正在检查某些元素是否不等于用户ID,但您可能要检查一些元素是否与之​​匹配:

let myArrayFiltered = allUsers.filter((user) => {
  return connectedUsers.some((el) => {
    return user._id === el;
  });
});

这正确地仅返回2个用户。