admin管理员组文章数量:1394740
I am trying to create a function in Javascript, that will allow me to filter the skills which the different candidates have, i have been told that this can be done in Javascript, the function called "filterCandidateBySkill" is where i want to filter the candidates skills, but ive had a look of this and as the skills is in an array i cant find anywhere online how to filter it. a lot of them have filters for single words, but not for arrays.
const newCandidates = [
{ name: "bob", skills: ["JavaScript", "Docker", "Ruby"] },
{ name: "ally", skills: ["Python", "AWS"] },
{ name: "joe", skills: ["JavaScript", "Azure"] },
{ name: "fred", skills: ["JavaScript", "Java"]},
];
function filterCandidateBySkill(candidates, skill) {
// where im confused
}
I am trying to create a function in Javascript, that will allow me to filter the skills which the different candidates have, i have been told that this can be done in Javascript, the function called "filterCandidateBySkill" is where i want to filter the candidates skills, but ive had a look of this and as the skills is in an array i cant find anywhere online how to filter it. a lot of them have filters for single words, but not for arrays.
const newCandidates = [
{ name: "bob", skills: ["JavaScript", "Docker", "Ruby"] },
{ name: "ally", skills: ["Python", "AWS"] },
{ name: "joe", skills: ["JavaScript", "Azure"] },
{ name: "fred", skills: ["JavaScript", "Java"]},
];
function filterCandidateBySkill(candidates, skill) {
// where im confused
}
Share
Improve this question
edited Jun 12, 2018 at 23:20
Programmerr
asked Jun 12, 2018 at 23:07
ProgrammerrProgrammerr
1,0413 gold badges10 silver badges11 bronze badges
1
- 1 What does this question have to do with Java? – CertainPerformance Commented Jun 12, 2018 at 23:07
1 Answer
Reset to default 8JS provides a filter method on arrays.
const newCandidates = [
{ name: "bob", skills: ["JavaScript", "Docker", "Ruby"] },
{ name: "ally", skills: ["Python", "AWS"] },
{ name: "joe", skills: ["JavaScript", "Azure"] },
{ name: "fred", skills: ["JavaScript", "Java"]},
];
function filterCandidateBySkill(candidates, skill) {
return newCandidates.filter(candidate => candidate.skills.includes(skill));
}
// Get only names
console.log(filterCandidateBySkill(newCandidates, 'JavaScript').map(candidate => candidate.name));
// Get entire objects
console.log(filterCandidateBySkill(newCandidates, 'JavaScript'));
本文标签: htmlHow to filter array of objects in JavascriptStack Overflow
版权声明:本文标题:html - How to filter array of objects in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744105112a2591029.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论