Recursively find data in array of objects
From a medium review;

This code belongs in a well named function
I always forget about destructuring in a filter, very nice
Mapping to an object automatically removes dupes, something I (ab)use in my counter
const findManager = (manager)  should probably be const findManager = (employee)
const findManager = (employee)  should probably be const findLineManagers = (employee)
From there, next should probably be manager , it is definitely not 'next' but more 'actual' ;)
You are not using semicolons, unless you understand all nuances, you should use them
It's more idiomatic in recursive programming to first check for the exit condition than then to check at the end if you should recurse or not

I struggled with this counter-proposal. I imagine in a codebase I maintain I would want my data structure to be linked. So this function gets a linked version of the data and then gets what is needed in what I think is an easier manner. YMMV.


// Initial list of names

function indexPeopleData(peopleData){
  //Index by name
  const indexedPeople = peopleData.reduce((out,person) =&gt; (out[person.name] = person, out), {});
  //Make reports point to a people object
  peopleData.forEach(person =&gt; person.reports = indexedPeople[person.reports]);
  return indexedPeople;
}

function getLineManagers(peopleData, names){
  const indexedPeople = indexPeopleData(peopleData);
  //Return list of employees and their managers
  let out = {}, name;
  while(name = names.pop()){
    out[name] = indexedPeople[name];
    if(out[name].reports.rank &gt; 1){
      names.push(out[name].reports.name);
    }
  }
  return Object.values(out);
}

var data = [
  {
    name: 'rebecca',
    reports: '',
    rank: 1
  },
  {
    name: 'will',
    reports: 'rebecca',
    rank: 2
  },
  {
    name: 'jose',
    reports: 'will',
    rank: 3
  },
  {
    name: 'tomas',
    reports: 'jose',
    rank: 4,
  },
  {
    name: 'matt',
    reports: 'jose',
    rank: 5
  },{
    name: 'alison',
    reports: 'john',
    rank: 5
  }
];

console.log(getLineManagers(data, ['tomas', 'matt']));