Home > Back-end >  Get most Senior
Get most Senior

Time:11-23

friends! I need your help.

A list of information about people is given.

An array containing the oldest person in the list must be returned. If several people are of the same highest age, then an array should be returned containing all of them.

The age is stored in the "age" field.

Input data:

const data =[
    { firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language: 'PHP' },
    { firstName: 'Odval', lastName: 'F.', country: 'Mongolia', continent: 'Asia', age: 38, language: 'Python' },
    { firstName: 'Emilija', lastName: 'S.', country: 'Lithuania', continent: 'Europe', age: 19, language: 'Python' },
    { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' },
]

const result = getMostSenior(data);

Output data:

console.log(result);
// [
//     { firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language: 'PHP' },
//     { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' },
// ]

My try:

const getMostSenior = humans => {

    let oldestHuman = humans.reduce((oldest, human) => {
        return oldest.age > human.age ? oldest : human;
    })
    return oldestHuman  
};

But this only returns an array containing the oldest person in the list.

Can't figure out how to return an array where multiple people have the same maximum age.

Please, help.

CodePudding user response:

This is not an efficient implementation though!

function getMostSenior(data){

  const ageArray = [];
  const agedPersons = {};
  data.forEach((item)=>{
    const age = item.age;
    ageArray.push(age);
    if(agedPersons.hasOwnProperty(age)){
      agedPersons[age].push(item);
    }
    else{
      agedPersons[age] = [];
      agedPersons[age].push(item);
    }
  });
  ageArray.sort();
  return [...agedPersons[ageArray[ageArray.length-1]]];
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Instead of pushing the object to the accumulator you should return only the age. You can then use filter on the data to get all those objects where the age matches the value of that variable.

const data=[{firstName:"Gabriel",lastName:"X.",country:"Monaco",continent:"Europe",age:49,language:"PHP"},{firstName:"Odval",lastName:"F.",country:"Mongolia",continent:"Asia",age:38,language:"Python"},{firstName:"Emilija",lastName:"S.",country:"Lithuania",continent:"Europe",age:19,language:"Python"},{firstName:"Sou",lastName:"B.",country:"Japan",continent:"Asia",age:49,language:"PHP"}];

function getMostSenior(data) {
  const oldest = data.reduce((oldest, c) => {
    return c.age > oldest ? c.age : oldest;
  }, 0);
  return data.filter(obj => obj.age === oldest);
}

console.log(getMostSenior(data));
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

The alternative to using reduce for this task would be to use a for...of loop to iterate over the array and update a variable if the age value of an object is greater than the current value of the variable.

const data=[{firstName:"Gabriel",lastName:"X.",country:"Monaco",continent:"Europe",age:49,language:"PHP"},{firstName:"Odval",lastName:"F.",country:"Mongolia",continent:"Asia",age:38,language:"Python"},{firstName:"Emilija",lastName:"S.",country:"Lithuania",continent:"Europe",age:19,language:"Python"},{firstName:"Sou",lastName:"B.",country:"Japan",continent:"Asia",age:49,language:"PHP"}];

function getMostSenior(data) {
  let oldest = 0;
  for (const { age } of data) {
    if (age > oldest) oldest = age;
  }
  return data.filter(obj => obj.age === oldest);
}

console.log(getMostSenior(data));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

I think this is little optimal way to solve your problem using Array Reduce

var data =[
    { firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language: 'PHP' },
    { firstName: 'Odval', lastName: 'F.', country: 'Mongolia', continent: 'Asia', age: 38, language: 'Python' },
    { firstName: 'Emilija', lastName: 'S.', country: 'Lithuania', continent: 'Europe', age: 19, language: 'Python' },
    { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' },
];


var output = data.reduce((a, c) => {

  if (a.length) {
    if (a[0].age < c.age)
      a = [c];
    else if (a[0].age == c.age)
      a.push(c)
  } else {
    a = [c];
  }

  return a;

}, []);

console.log(output);
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related