I'm working on a coding problem for university and can't figure this out.
How can I get a new array from the above array with only applicants that have JavaScript listed as a skill using builtin methods only?
The array looks like this:
const NewApplicants = [
{ name: "Jimmy", skills: ["Docker", "JavaScript", "Ruby"] },
{ name: "Dave", skills: ["AWS", "Python"] },
{ name: "Frankie", skills: ["Azure", "JavaScript"] },
{ name: "Liam", skills: ["Java", "JavaScript"] },
{ name: "Fred", skills: ["JavaScript", "AWS"] },
{ name: "Sara", skills: ["PHP", "AWS"] },
{ name: "Matt", skills: [".Net", "PHP", "Docker"] },
];
... and the new array should look like this:
const NewJavaScriptApplicants = [
{ name: "Jimmy", skills: ["Docker", "JavaScript", "Ruby"] },
{ name: "Frankie", skills: ["Azure", "JavaScript"] },
{ name: "Liam", skills: ["Java", "JavaScript"] },
{ name: "Fred", skills: ["JavaScript", "AWS"] },
];
I'm working on a coding problem for university and can't figure this out.
How can I get a new array from the above array with only applicants that have JavaScript listed as a skill using builtin methods only?
The array looks like this:
const NewApplicants = [
{ name: "Jimmy", skills: ["Docker", "JavaScript", "Ruby"] },
{ name: "Dave", skills: ["AWS", "Python"] },
{ name: "Frankie", skills: ["Azure", "JavaScript"] },
{ name: "Liam", skills: ["Java", "JavaScript"] },
{ name: "Fred", skills: ["JavaScript", "AWS"] },
{ name: "Sara", skills: ["PHP", "AWS"] },
{ name: "Matt", skills: [".Net", "PHP", "Docker"] },
];
... and the new array should look like this:
const NewJavaScriptApplicants = [
{ name: "Jimmy", skills: ["Docker", "JavaScript", "Ruby"] },
{ name: "Frankie", skills: ["Azure", "JavaScript"] },
{ name: "Liam", skills: ["Java", "JavaScript"] },
{ name: "Fred", skills: ["JavaScript", "AWS"] },
];
Share
Improve this question
edited Aug 15, 2018 at 19:29
user4639281
asked Aug 15, 2018 at 18:01
Jake WilcoxJake Wilcox
1761 gold badge2 silver badges11 bronze badges
0
2 Answers
Reset to default 10Use Array#filter
and Array#includes
like so:
const NewApplicants = [
{ name: "Jimmy", skills: ["Docker", "JavaScript", "Ruby"] },
{ name: "Dave", skills: ["AWS", "Python"] },
{ name: "Frankie", skills: ["Azure", "JavaScript"] },
{ name: "Liam", skills: ["Java", "JavaScript"] },
{ name: "Fred", skills: ["JavaScript", "AWS"] },
{ name: "Sara", skills: ["PHP", "AWS"] },
{ name: "Matt", skills: [".Net", "PHP", "Docker"] },
];
const JavaScriptApplicants = NewApplicants.filter(e => e.skills.includes("JavaScript"));
console.log(JavaScriptApplicants)
The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.
- https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
The
includes()
method determines whether an array includes a certain element, returning true or false as appropriate.
- https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
You can use the filter
javascript method to loop through the objects and apply this condition:
var filteredData = NewApplicants.filter(function(applicant) {
return applicant.skills.includes('JavaScript');
});