Search code examples
javascriptarraysclassinstance

Question on how to work with class instances inside an array


So i am learning how to work with classes and class instances and have created a scenario where i have a class Employee that is extended by three other classes that represent employees from different departments. I already managed to create a function that allows me to create a new instance of any class and push it into an array containing all existing employees.

So now i am trying to play around and iterate over the array and create new functions that allow me to access a specific value of any instance.

for example the first one i am trying to do is a function that return true or false if an employee is working remotely:

function areTheyRemote(employee){
    if (employee.workplace === 'home'){
        return true;
    } else {
        return false;
    }
}

I have no idea and couldn't find any answers online on how to do it, hoping you could shed a light on me. Cheers


Solution

  • Without seeing your class code for the employee it should be something like this:

    function Employee(name, age, workplace) {
      this.name = name
      this.age = age
      this.workplace = workplace
    }
    
    const bob = new Employee('bob', 22, 'remote')
    
    function areTheyRemote(employee) {
      if (employee.workplace === 'remote') {
        return true;
      } else {
        return false;
      }
    }
    
    console.log(areTheyRemote(bob))