Advertisement
didkoslawow

Untitled

Mar 4th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function companyUsers(input) {
  2.   const companiesInfo = [...input];
  3.   const companies = {};
  4.  
  5.   companiesInfo.forEach((currentCompany) => {
  6.     const company = currentCompany.split(' -> ');
  7.     const companyName = company[0];
  8.     const id = company[1];
  9.  
  10.     if (!companies.hasOwnProperty(companyName)) {
  11.       companies[companyName] = [id];
  12.     } else {
  13.       const currentID = id;
  14.       companies[companyName].push(currentID);
  15.     }
  16.  
  17.     companies[companyName] = [...new Set(companies[companyName])];
  18.   });
  19.  
  20.   const sortedCompanies = Object.entries(companies).sort((a, b) =>
  21.     a[0].localeCompare(b[0])
  22.   );
  23.  
  24.   for (const company of sortedCompanies) {
  25.     const name = company[0];
  26.     const ids = company[1];
  27.     console.log(name);
  28.     ids.forEach(id => {
  29.         console.log(`-- ${id}`);
  30.     });
  31.   }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement