1

So I have an array which looks like the one displayed below:

const newTeachers = [
      { firstName: "Steve", subjects: ["English", "Maths", "History"] },
      { firstName: "Celia", subjects: ["Maths", "Science"] },
    ];

I want to create a function which takes in 2 parameters 1 being the array and 2nd being the filter string value.

function fliterSubject(teachers, subject) {

}

I have looked into the inbuilt way which is to implement this line of code:

 return newCandidates.filter(teacher=> teacher.subjects.includes(subject));

However I want to understand how I can filter it manually instead of using the inbuilt filter function.

6
  • Just iterate over it with for loop and if currently iterated element matches or unmatches the condition push it to new array or not. Commented Dec 2, 2018 at 13:58
  • 1
    You can loop through the array and push only if certain condition matches. but why you wanna do that ? Commented Dec 2, 2018 at 13:59
  • @CodeManiac I know there is the inbuilt filter function but I want to grasp the manual way of doing it to learn it. Commented Dec 2, 2018 at 14:04
  • It's good if you just wanna know how it works but in code it's preferable to use inbuilt one Commented Dec 2, 2018 at 14:06
  • @yes , I am just curious about it so am trying that way. Commented Dec 2, 2018 at 14:09

2 Answers 2

2

Please check this.

var filterSubject = function(teachers, subject) {
  var filteredTeachers = [];
  for (var i=0; i<=teachers.length-1; i++) {
    var teacher = teachers[i];
    for (var j=0; j<=teacher.subjects.length-1; j++) {
      if (teacher.subjects[j].toLowerCase()== subject.toLowerCase()) {
        filteredTeachers.push(teacher);
      }
    }
  }
  return filteredTeachers;
}

Please check an example @ CodePen https://codepen.io/animatedcreativity/pen/213ddd0a66b73b05c907da72c8aef3e3

Sign up to request clarification or add additional context in comments.

3 Comments

why are you creating another var teacher. Is it because your are reading the array within teachers array right?
@sam251: teacher variable represents each teacher one by one in the teachers loop so that we can loop through all the subjects of each teacher and match with the supplied subject argument. If argument subject matches the teacher's subject in the loop, then that teacher is added to filteredTeachers.
I see that makes sense. Thank you
2

Explanation

You can achieve this with a simple for loop.

So you need to loop the array you're getting as argument in function.

Create a temporary array and push the values into it only if certain condition matches and return that array from function.

So something like this :-

function manuamMap(input, search){

Let temp = [];

for(let i=0; i<limit; i++) {
  if(some confition){
    temp.push();
  }
}

return temp
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.