skip to Main Content

Could someone help me understand why:

const people = [{
    name: "Carly",
    yearOfBirth: 1942,
    yearOfDeath: 1970,
  },
  {
    name: "Ray",
    yearOfBirth: 1962,
    yearOfDeath: 2011,
  },
  {
    name: "Jane",
    yearOfBirth: 1912,
    yearOfDeath: 1941,
  },
];



const findTheOldest = function(array) {
  let alivePeople = array.filter(function(person) {
    console.log(person.yearOfDeath);
    if (person.yearOfDeath === true) {
      console.log(person);
      return true;
    }
  });
  return alivePeople;
};

console.log(findTheOldest(people))

Is showing https://i.imgur.com/7qQXLJ0.png?

I was expecting it to return all the objects in the people array. I was trying to write code that would filter out people without a year of death.

3

Answers


  1. Your code doesn’t work because person.yearOfDeath does not equal true for any of the objects in the array.

    You should check whether each object has the yearOfDeath property, which can be done with Object.hasOwn, Object#hasOwnProperty, or the in operator. Object.hasOwn is recommended for modern code if there is no need to check the prototype chain.

    const people = [{
        name: "Carly",
        yearOfBirth: 1942,
        yearOfDeath: 1970,
      },
      {
        name: "Ray",
        yearOfBirth: 1962,
        yearOfDeath: 2011,
      },
      {
        name: "Jane",
        yearOfBirth: 1912,
        yearOfDeath: 1941,
      },
      {
        name: "John",
        yearOfBirth: 2000
      }
    ];
    let res = people.filter(o => Object.hasOwn(o, 'yearOfDeath'));
    console.log(res);
    Login or Signup to reply.
  2. This does the job

    const people = [
      {
        name: "Carly",
        yearOfBirth: 1942,
        yearOfDeath: 1970,
      },
      {
        name: "Ray",
        yearOfBirth: 1962,
        yearOfDeath: 2011,
      },
      {
        name: "Jane",
        yearOfBirth: 1912,
        yearOfDeath: 1941,
      },
      {
        name: "John",
        yearOfBirth: 1920,
      }
    ];
    
    
    
    const findTheOldest = function(array) {
    let alivePeople = array.filter(function (person) {
      if (person.yearOfDeath != undefined) {
        return true;
      }
    });
    return alivePeople; 
    };
    
    console.log(findTheOldest(people))
    

    Output

    [
      { name: 'Carly', yearOfBirth: 1942, yearOfDeath: 1970 },
      { name: 'Ray', yearOfBirth: 1962, yearOfDeath: 2011 },
      { name: 'Jane', yearOfBirth: 1912, yearOfDeath: 1941 }
    ]
    
    Login or Signup to reply.
  3. Its doing exactly as you asked it

    function filterOutDeadPeople(array) {
        let alivePeople = array.filter(function (person) {
          if (person.yearOfDeath === true) {
            return true;
          }
        });
        return alivePeople;
      }
    
    console.log(filterOutDeadPeople(people));
    

    Will filter out dead people this is not the what you want as its to filter out alive people

    function filterOutAlivePeople(array) {
        let deadPeople = array.filter(function (person) {
          if (person.yearOfDeath === false) {
            return true;
          }
        });
        return deadPeople;
      }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search