Todorov_Stanimir

10. Systems Register

Jun 20th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function systemComponents(systems) {
  2.     let systemsOutput = {};
  3.     systems.forEach(system => {
  4.         let [systemName, componentName, subcomponentName] = system.split(' | ')
  5.         if (!(systemName in systemsOutput)) {
  6.             systemsOutput[systemName] = {};
  7.         }
  8.         if (!(componentName in systemsOutput[systemName])) {
  9.             systemsOutput[systemName][componentName] = [];
  10.         }
  11.         systemsOutput[systemName][componentName].push(subcomponentName);
  12.     });
  13.  
  14.     systemsOutput = Object.entries(systemsOutput);
  15.     systemsOutput.forEach(system => {
  16.         system[1] = Object.entries(system[1])
  17.     })
  18.     sortSystems(systemsOutput);
  19.     sortComponents(systemsOutput);
  20.  
  21.     function sortSystems() {
  22.         systemsOutput.sort((a, b) => {
  23.             if (a[1].length > b[1].length || a[1].length < b[1].length) {
  24.                 return b[1].length - a[1].length;
  25.             } else {
  26.                 return a[0].localeCompare(b[0]);
  27.             }
  28.         });
  29.     }
  30.  
  31.     function sortComponents() {
  32.         systemsOutput.forEach(system => {
  33.             system[1].sort((a, b) => {
  34.                 return b[1].length - a[1].length;
  35.             });
  36.         });
  37.     }
  38.     systemsOutput.forEach(system => {
  39.         console.log(system[0]);
  40.         system[1].forEach(component => {
  41.             console.log(`|||${component[0]}`);
  42.             component[1].forEach(subcomponent => {
  43.                 console.log(`||||||${subcomponent}`);
  44.             });
  45.         });
  46.     });
  47. }
Advertisement
Add Comment
Please, Sign In to add comment