简体   繁体   English

比较两个 arrays 的不同值并在 JavaScript 中返回一个过滤数组

[英]comparing different values of two arrays and returning one filtered array in JavaScript

I am trying to compare the values of two different arrays, if the user.id of the volunteer iteration matches the id of the userVolunteer iteration, I want that entry to be filtered out of the volunteers array我正在尝试比较两个不同 arrays 的值,如果志愿者迭代的user.iduserVolunteer迭代的id匹配,我希望将该条目从志愿者数组中过滤掉

current attempt:当前尝试:

  const newBirds = volunteers.filter((volunteer) => {
    return volunteer.user_id !== userVolunteers.some((vol) => vol.id)
  });

newBirds currents returns the volunteers array without filtering any of the entries? newBirds currents 返回志愿者数组而不过滤任何条目? Any advice?有什么建议吗?

You need to put the logic of comparison inside the some callback:您需要将比较逻辑放在some回调中:

const newBirds = volunteers.filter((volunteer) => {
  return userVolunteers.some((vol) => volunteer.user_id !== vol.id)
});

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

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