我正在尝试修改filterCandidateBySkill()函数,以便仅通过JavaScript技能来过滤候选对象。
我是javascript新手,不熟悉其语法。如果有人可以帮助我,那就太好了:)
<script type="text/javascript">
const newCandidates = [
{ name: "Reece", skills: ["JavaScript", "Docker", "Java"] },
{ name: "Joanna", skills: ["Python", "Scala"] },
{ name: "Dan", skills: ["JavaScript", "Azure"] },
{ name: "Simone", skills: ["JavaScript", "Java"] },
{ name: "Ned", skills: ["JavaScript", "AWS"] },
{ name: "Yuhi", skills: ["PHP", "JavaScript"] },
{ name: "Jack", skills: ["PHP", ".Net", "C++"] },
];
function removeRowsFromTable(table) {
const rows = table.getElementsByTagName("tr");
while (rows.length > 1) {
table.deleteRow(1);
}
}
function insertCandidate(tbody, name, skills) {
const newRow = tbody.insertRow();
const nameCell = newRow.insertCell();
const skillCell = newRow.insertCell();
const candidateName = document.createTextNode(name);
const candidateSkills = document.createTextNode(skills.join(', '));
nameCell.appendChild(candidateName);
skillCell.appendChild(candidateSkills);
}
function addCandidatesToTable(table, candidates) {
candidates.forEach(candidate => insertCandidate(table, candidate.name, candidate.skills));
}
function filterCandidateBySkill(candidates, skill) {
// INSERT YOUR LOGIC HERE <-------------------------
return candidates;
}
const candidatesTable = document.getElementById("candidates_example");
const newCandidatesTable = candidatesTable.cloneNode(true);
removeRowsFromTable(newCandidatesTable);
const newTbody = newCandidatesTable.getElementsByTagName('tbody')[0];
const filteredCandidates = filterCandidateBySkill(newCandidates, 'JavaScript')
addCandidatesToTable(newTbody, filteredCandidates)
document.body.appendChild(newCandidatesTable);
答案 0 :(得分:0)
您可以使用filter()
和includes()
来获取过滤后的结果。过滤器需要一个返回布尔值的函数,该布尔值指示是否应包含该元素。 includes()
传递该布尔值:
const newCandidates = [
{ name: "Reece", skills: ["JavaScript", "Docker", "Java"] },
{ name: "Joanna", skills: ["Python", "Scala"] },
{ name: "Dan", skills: ["JavaScript", "Azure"] },
{ name: "Simone", skills: ["JavaScript", "Java"] },
{ name: "Ned", skills: ["JavaScript", "AWS"] },
{ name: "Yuhi", skills: ["PHP", "JavaScript"] },
{ name: "Jack", skills: ["PHP", ".Net", "C++"] },
];
function filterCandidateBySkill(candidates, skill) {
// INSERT YOUR LOGIC HERE <-------------------------
return candidates.filter(cand => cand.skills.includes(skill));
}
console.log(filterCandidateBySkill(newCandidates, 'JavaScript'))
答案 1 :(得分:0)
您可以简单地使用Array.prototype.filter()和Array.prototype.includes()来筛选出具有 JavaScript 技能
的候选人
const newCandidates = [
{ name: "Reece", skills: ["JavaScript", "Docker", "Java"] },
{ name: "Joanna", skills: ["Python", "Scala"] },
{ name: "Dan", skills: ["JavaScript", "Azure"] },
{ name: "Simone", skills: ["JavaScript", "Java"] },
{ name: "Ned", skills: ["JavaScript", "AWS"] },
{ name: "Yuhi", skills: ["PHP", "JavaScript"] },
{ name: "Jack", skills: ["PHP", ".Net", "C++"] },
];
function filterCandidateBySkill(candidates, skill) {
return candidates.filter(candidate=>candidate.skills.includes(skill));
}
console.log(filterCandidateBySkill(newCandidates, 'JavaScript'))