nikolayneykov

Untitled

Mar 5th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function systemRegister(params) {
  2.     let systems = {};
  3.  
  4.     for (let param of params) {
  5.         let tokens = param.split(' | ').filter(x => x !== '');
  6.         let currentSystem = tokens[0];
  7.         let currentComponent = tokens[1];
  8.         let currentSubcomponent = tokens[2];
  9.  
  10.         if (!systems.hasOwnProperty(currentSystem)) {
  11.             systems[currentSystem] = {};
  12.         }
  13.         if (!systems[currentSystem].hasOwnProperty(currentComponent)) {
  14.             systems[currentSystem][currentComponent] = [];
  15.         }
  16.         systems[currentSystem][currentComponent].push(currentSubcomponent);
  17.     }
  18.  
  19.     let sortedSystemKeys = Object.keys(systems).sort(function (a, b) {
  20.         let result = Object.keys(systems[b]).length - Object.keys(systems[a]).length;
  21.         if (result === 0) {
  22.             result = a.localeCompare(b);
  23.         }
  24.         return result;
  25.     });
  26.  
  27.     for (let systemKey of sortedSystemKeys) {
  28.         console.log(systemKey);
  29.         let sortedComponentKeys = Object.keys(systems[systemKey]).sort(function (a, b) {
  30.             return systems[systemKey][b].length - systems[systemKey][a].length;
  31.         });
  32.         for (let componentKey of sortedComponentKeys) {
  33.             console.log('|||' + componentKey);
  34.             let subcomponentKeys = systems[systemKey][componentKey];
  35.             for (let subcomponentKey of subcomponentKeys) {
  36.                 console.log('||||||' + subcomponentKey);
  37.             }
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment