如何使用filter()方法在数组中的两个数组之间进行迭代

时间:2018-11-19 19:42:43

标签: javascript node.js google-cloud-functions

如何获取进入.map()块的信息? 有可能做到这一点,还是我需要以其他方式解决这个问题?

var orderCompetences = [];
var nActiveApplicants = [];
function MatchCompetences() {
 var _applicantCompetenceResults = nActiveApplicants.filter(xApplicant => {
   xApplicant.applicantCompetences.map(applComp =>
   orderCompetence.map(orderComp => {
     console.log(applComp ); //never gets called
     console.log(orderComp);//never gets called
     return applComp === orderComp;
  }));
});

 return Promise.all(_applicantCompetenceResults)
 .then(resp => {
   console.log(resp); // Never gets called
   return 1;
 })
 .catch(err => {
   console.log(err);
   return 0;
 });
}

nActiveApplicants内的对象

nApplicant = {
  applicantID: "",
  applicantPeriods: [],
  applicantCompetences: [],
  applicantFullAddress: "",
  applicantDuration: ""
};

2 个答案:

答案 0 :(得分:0)

我怀疑您想要这样的内容,尽管我不清楚您为什么将其包装在Promise.all中,因为我敢肯定_applicantCompetenceResults不会是一个Promises数组:

var orderCompetences = [];
var nActiveApplicants = [];
function MatchCompetences() {
  var _applicantCompetenceResults = nActiveApplicants.filter(xApplicant => 
    xApplicant.applicantCompetences.some(applComp => orderCompetences.includes(applComp))
  );

  return Promise.all(_applicantCompetenceResults)
    .then(resp => {
      console.log(resp);
      return 1;
    })
    .catch(err => {
      console.log(err);
      return 0;
    });
}

如果这不能解决问题,您可能想尝试解释您希望实现的目标,而不是深入了解正在使用的代码。

答案 1 :(得分:0)

我假设您希望使用applicantCompetences方法搜索orderCompetences中的某些内容find来查找有效结果,然后对其进行过滤。我将剩余的记录映射到Promises中。

function MatchCompetences() {

  var nActiveApplicants = [{
      applicantID: "abcd",
      applicantPeriods: [],
      applicantCompetences: ['can read', 'can talk', 'useless mostly'],
      applicantFullAddress: "",
      applicantDuration: ""
    },
    {
      applicantID: "efgh",
      applicantPeriods: [],
      applicantCompetences: ['can read', 'can talk', 'singer mostly', 'it-will-do'],
      applicantFullAddress: "",
      applicantDuration: ""
    }
  ];

  var orderCompetence = ['it-will-do'];

  var _applicantCompetenceResults = nActiveApplicants.filter(xApplicant => {

    return typeof xApplicant.applicantCompetences.find(elem => {
      return orderCompetence.indexOf(elem) !== -1;
    }) !== 'undefined'

  }).map(item => {

    return new Promise(resolve => {
      resolve(item);
    })

  });

  return Promise.all(_applicantCompetenceResults)
    .then(resp => {
      console.log('remaining applicants', resp); // Never gets called
      return 1;
    })
    .catch(err => {
      console.log(err);
      return 0;
    });
}

MatchCompetences()