How to find the oldest person in People Array using javascript - Stack Overflow

I'm trying retrieve the oldest person from the array using the .reduce() method in javascript. The

I'm trying retrieve the oldest person from the array using the .reduce() method in javascript. The array is below. What I'm struggling with is writing the correct syntax for the .reduce method to acplish this. The code I'm using is below and I keep getting the result 'undefined'. I've been working with the code for a little while and feel I might be making a simple mistake but a nudge in the right direction would be helpful!

let findTheOldest = function(people) {
  let total = 0
     people.reduce((prev, curr) => {
    if ((curr.yearOfDeath - curr.yearOfBirth) > total) {
    total = (curr.yearOfDeath - curr.yearOfBirth);
    }
    return prev;
  }, 0);
}
console.log(findTheOldest(people));
const people = [
      {
        name: 'Carly',
        yearOfBirth: 1942,
        yearOfDeath: 1970,
      },
      {
        name: 'Ray',
        yearOfBirth: 1962,
        yearOfDeath: 2011
      },
      {
        name: 'Jane',
        yearOfBirth: 1912,
        yearOfDeath: 1941
      },
    ]

I'm trying retrieve the oldest person from the array using the .reduce() method in javascript. The array is below. What I'm struggling with is writing the correct syntax for the .reduce method to acplish this. The code I'm using is below and I keep getting the result 'undefined'. I've been working with the code for a little while and feel I might be making a simple mistake but a nudge in the right direction would be helpful!

let findTheOldest = function(people) {
  let total = 0
     people.reduce((prev, curr) => {
    if ((curr.yearOfDeath - curr.yearOfBirth) > total) {
    total = (curr.yearOfDeath - curr.yearOfBirth);
    }
    return prev;
  }, 0);
}
console.log(findTheOldest(people));
const people = [
      {
        name: 'Carly',
        yearOfBirth: 1942,
        yearOfDeath: 1970,
      },
      {
        name: 'Ray',
        yearOfBirth: 1962,
        yearOfDeath: 2011
      },
      {
        name: 'Jane',
        yearOfBirth: 1912,
        yearOfDeath: 1941
      },
    ]
Share Improve this question asked Apr 11, 2020 at 2:18 rueeazyrueeazy 1191 silver badge9 bronze badges 3
  • Note that the first argument in reduce is not the previous value but accumulator. The accumulator accumulates callback's return values. – kiranvj Commented Apr 11, 2020 at 2:30
  • Are you dead-set on using reduce? – Joel Hager Commented Apr 11, 2020 at 2:56
  • Hi Joel, if you have anything better in mind, please share. I have shared an answer which does not use reduce. You never know what new thing you can learn! – LearningEveryday Commented Apr 11, 2020 at 3:07
Add a ment  | 

4 Answers 4

Reset to default 3

You can use a parator to sort the whole array in the order you want as well and get the first element. Ironically, the example provided in this link is almost the same as we have here :
http://www.mattmorgante./technology/javascript-sort-pare

Does not use reduce but thought this was interesting.

    const people = [{
        name: 'Carly',
        yearOfBirth: 1942,
        yearOfDeath: 1970,
      },
      {
        name: 'Ray',
        yearOfBirth: 1962,
        yearOfDeath: 2011
      },
      {
        name: 'Jane',
        yearOfBirth: 1912,
        yearOfDeath: 1941
      },
    ]
    const oldestPeople = people.sort(function(a,b) {
  const lastPerson = a.yearOfDeath - a.yearOfBirth;
  const nextPerson = b.yearOfDeath - b.yearOfBirth;
  if (lastPerson > nextPerson) {
    return -1;
  } else {
    return 1;
  }
});

console.log(people[0]);

You have undefined because the function findTheOldest just return nothing.
If you want to keep your function and using your variable total (not very optimised) you need to return the oldestPerson like this:

let findTheOldest = function(people) {
  let total = 0
  let OldestPerson = {}
  people.reduce((prev, curr) => {
    if ((curr.yearOfDeath - curr.yearOfBirth) > total) {
      total = (curr.yearOfDeath - curr.yearOfBirth);
      OldestPerson = curr
    }
    return prev;
  })
  return OldestPerson
}

For reduce to work, you'd have to either re-calculate the age of the accumulator every time (somewhat inelegant), or save it in an outside variable. Then, return inside the callback depending on whether the accumulator or the current item being iterated over has the greater age:

const getAge = ({ yearOfBirth, yearOfDeath }) => yearOfDeath - yearOfBirth;
const findTheOldest = function(people) {
  let accumAge = getAge(people[0]);
  return people.reduce((accum, curr) => {
    const currAge = getAge(curr);
    if (currAge > accumAge) {
      accumAge = currAge;
      return curr;
    }
    return accum;
  });
}

const people = [{
    name: 'Carly',
    yearOfBirth: 1942,
    yearOfDeath: 1970,
  },
  {
    name: 'Ray',
    yearOfBirth: 1962,
    yearOfDeath: 2011
  },
  {
    name: 'Jane',
    yearOfBirth: 1912,
    yearOfDeath: 1941
  },
]
console.log(findTheOldest(people));

While it's possible with reduce, I think it would be somewhat ugly and not entirely appropriate

You can use reduce. While it is ugly it shortens the code quite a bit =)

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

    const res = people.reduce((prevVal,curVal) => {
      return prevVal.yearOfDeath-prevVal.yearOfBirth < curVal.yearOfDeath-curVal.yearOfBirth ? curVal: prevVal;
    })
    console.log(res.name,res.yearOfDeath-res.yearOfBirth);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745149891a4613820.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信