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



如何让它进入.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: ""
};

我假设您希望某些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()

我怀疑你想要这样的东西,尽管我不清楚你为什么要把它包装在Promise.all中,因为我很确定_applicantCompetenceResults不会是Promise:的数组

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;
});
}

如果这对你没有好处,你可能想试着解释你希望实现的目标,而不是深入了解你正在使用的代码的细节。

相关内容

  • 没有找到相关文章

最新更新