简体   繁体   中英

Checking if a Key/Value pair exists in an Array of Objects - Javascript

I am trying to find the oldest person in an array of objects. In the below example, my final answer should return 'Carly' because she has been alive for the longest.

My code is returning Ray because it doesn't recognise that Carly is still Alive.

I have tried nesting my code in an if/else statement which checks to see if the yearOfDeath value exists but clearly something is wrong as it is still returning the Ray object.

let findTheOldest = function(arr) {
    var oldest = 0;
    var oldestPerson = {};
    var today = new Date();
    var yyyy = today.getFullYear();


    arr.reduce((total, person) => {

      if (person.yearOfDeath){
         if ((person.yearOfDeath - person.yearOfBirth) > oldest){
            oldest = person.yearOfDeath - person.yearOfBirth;
            oldestPerson = person;
           } 
      } 

      else {
         person.yearOfDeath = yyyy;
         if ((person.yearOfDeath - person.yearOfBirth) > oldest){
            oldest = person.yearOfDeath - person.yearOfBirth;
            oldestPerson = person;
           }
      }

   });

     console.table(oldestPerson);
     console.table(arr);
}



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

findTheOldest(people);

Appreciate any help?.

I have done it like this:

 const people = [ { name: 'Carly', yearOfBirth: 1066, }, { name: 'Ray', yearOfBirth: 1962, yearOfDeath: 2011 }, { name: 'Jane', yearOfBirth: 1912, yearOfDeath: 1941 }, ]; function findOldest(arr){ let ages = people.map(el => { let age = el.yearOfDeath == undefined? new Date().getFullYear() - el.yearOfBirth: el.yearOfDeath - el.yearOfBirth; return age; }) let maxAge = Math.max(...ages); let index = ages.indexOf(maxAge); return arr[index]; } console.log(findOldest(people));

If you replace reduce by a for loop, this works. Not sure why you needed reduce call there.

 let findTheOldest = function(arr) { var oldest = 0; var oldestPerson = {}; var today = new Date(); var yyyy = today.getFullYear(); for (let i in arr) { const person = arr[i]; if (person.yearOfDeath){ if ((person.yearOfDeath - person.yearOfBirth) > oldest){ oldest = person.yearOfDeath - person.yearOfBirth; oldestPerson = person; } } else { person.yearOfDeath = yyyy; if ((person.yearOfDeath - person.yearOfBirth) > oldest){ oldest = person.yearOfDeath - person.yearOfBirth; oldestPerson = person; } } } return oldestPerson; } const people = [ { name: 'Carly', yearOfBirth: 1066, }, { name: 'Ray', yearOfBirth: 1962, yearOfDeath: 2011 }, { name: 'Jane', yearOfBirth: 1912, yearOfDeath: 1941 }, ]; const oldest = findTheOldest(people); console.log(oldest); // shows {name: "Carly", yearOfBirth: 1066}

Take into account you're modifying the original array elements (setting yearOfDeath ). In case you want to avoid that, compare directly to the full year when the person is alive, instead of setting it first to the object.

Improving the code a little bit, that's what I do there:

if ((person.yearOfDeath || currentYear) - person.yearOfBirth > oldest) --> in case yearOfDeath is undefined , current year will be used to compare.

I also applied reduce to iterate through all the elements, given you say it was actually required. I used an object with two accumulators there. Also, check you need to initialize it as well.

 let findTheOldest = function(arr) { const currentYear = new Date().getFullYear(); const result = arr.reduce(({ oldest, oldestPerson }, person) => { if ((person.yearOfDeath || currentYear) - person.yearOfBirth > oldest) { oldest = (person.yearOfDeath || currentYear) - person.yearOfBirth; oldestPerson = person; } return { oldest, oldestPerson }; }, { oldest: 0, oldestPerson: {} }); return result.oldestPerson; } const people = [ { name: 'Carly', yearOfBirth: 1066, }, { name: 'Ray', yearOfBirth: 1962, yearOfDeath: 2011 }, { name: 'Jane', yearOfBirth: 1912, yearOfDeath: 1941 }, ]; const oldest = findTheOldest(people); console.log(oldest); // shows {name: "Carly", yearOfBirth: 1066}

Best to use .sort() instead of .reduce() here. Sort by yearOfDeath - yearOfBirth in descending order and return the first element of the sorted array:

let findTheOldest = function(arr) {
  var oldest = 0;
  var oldestPerson = [];
  var today = new Date();
  var yyyy = today.getFullYear();


  oldestPerson = arr.sort((personA, personB) => {
    if (personA.yearOfDeath && personB.yearOfDeath) {
      return (personA.yearOfDeath - personA.yearOfBirth) < (personB.yearOfDeath - personB.yearOfBirth) ? 1 : -1;
      }
    else {
    if (!personA.yearOfDeath) personA.yearOfDeath = yyyy;
    if (!personB.yearOfDeath) personB.yearOfDeath = yyyy;
    return (personA.yearOfDeath - personA.yearOfBirth) < (personB.yearOfDeath - personB.yearOfBirth) ? 1 : -1;
    }
  })[0];

  console.log(oldestPerson); //logs the Carly object
  console.log(oldestPerson.name); //logs "Carly"

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM