Higher Order Function with filter and sort that finds specified property in object's array

Viewed 157

I'm struggling to understand higher-order functions. I have an objects array and want to return the developer out of the following list that is the oldest. In case some have the same age, I want to return all of them.

let input = [
  { 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' },
];

Output I'm looking for:

let output =  [
  { firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language: 'PHP' },
  { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' },
];

Here is my code:

let oldDevFirst = input.sort((a,b)=>ages= b.age-a.age)

let allOdldDev = oldDevFirst.filter(el=> el === el.age[0]) //oldest developer at el.age[0]

Basically I sorted the list with 'oldDevFirst' so that the oldest developer comes first. However, I don't know how I can compare every property to the first property to return all the developers, that have the same oldest age. Thanks for reading or even helping a beginner out!

4 Answers

You need to do this:

let input = [
  { 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' },
];

let oldDevFirst = input.sort((a,b)=>ages= b.age-a.age)
let maxAge = oldDevFirst[0].age;
let allOdldDev = oldDevFirst.filter(el=> el.age === maxAge)

console.log(allOdldDev);

A better way would be finding the highest age, and then getting the list:

let input = [
  { 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' },
];

let maxAge = Math.max.apply(Math, input.map(function(o) { return o.age; }))

let allOdldDev = input.filter(el=> el.age === maxAge)

console.log(allOdldDev);

You could take a single loop approach and look only for age and collect greater in a new array or same age in the same result set.

let input = [{ 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' }],
    result = input.reduce((r, o, i) => {
        if (!i || r[0].age < o.age) return [o];
        if (r[0].age === o.age) r.push(o);
        return r;    
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

let input = [
  {
    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 heightage = input.map((i) => i.age)[0];
input.filter((item) => item.age === heightage);
  1. use Math.max find the highest age value
  2. use array filter to select the equal highest age value people
let input = [
  {
    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 tMax = Math.max(...input.map(item => item.age));
const result = input.filter(item => item.age == tMax);
console.log(result);
Related