Advertisement
kstoyanov

07. Company Users

Jul 13th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(args) {
  2.   const companies = {};
  3.  
  4.   args.forEach((element) => {
  5.     const [company, emplId] = element.split(' -> ');
  6.  
  7.     if (!Object.prototype.hasOwnProperty.call(companies, company)) {
  8.       companies[company] = [];
  9.     }
  10.  
  11.     if (Object.prototype.hasOwnProperty.call(companies, company)) {
  12.       const isFind = companies[company].find((empl) => empl === emplId);
  13.       if (isFind === undefined) {
  14.         companies[company].push(emplId);
  15.       }
  16.     }
  17.   });
  18.  
  19.   const sortByName = Object.entries(companies).sort((a, b) => a[0].localeCompare(b[0]));
  20.  
  21.   sortByName.forEach((element) => {
  22.     const [companyName, employeeId] = element;
  23.  
  24.     console.log(`${companyName} \n-- ${employeeId.join('\n-- ')}`);
  25.   });
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement