kstoyanov

10. Systems Register js fundamentals 80%

Jul 4th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function systemRegister(args) {
  2.   const obj = [];
  3.  
  4.   args.forEach((element) => {
  5.     const [systemName] = element.trim().split('|');
  6.  
  7.     const systemObj = {};
  8.     systemObj.system = systemName;
  9.     systemObj.value = [];
  10.  
  11.     const dub = obj.filter((item) => item.system.localeCompare(systemName) === 0).length;
  12.     if (dub === 0) {
  13.       obj.push(systemObj);
  14.     }
  15.   });
  16.  
  17.   args.forEach((element) => {
  18.     const [systemName, componentName] = element.trim().split('|');
  19.  
  20.  
  21.     obj.forEach((el) => {
  22.       const componentObj = {};
  23.  
  24.       componentObj.component = componentName;
  25.       componentObj.value = [];
  26.  
  27.       const dubComponent = el.value.filter((item) => item.component.localeCompare(componentName) === 0).length;
  28.       if (el.system.localeCompare(systemName) === 0) {
  29.         if (dubComponent === 0) {
  30.           el.value.push(componentObj);
  31.         }
  32.       }
  33.     });
  34.   });
  35.  
  36.   args.forEach((element) => {
  37.     const [systemName, componentName, subcomponentName] = element.trim().split('|');
  38.  
  39.  
  40.     obj.forEach((el) => {
  41.       el.value.forEach((item) => {
  42.         const dubSubComponent = item.value.filter((subComponent) => subComponent.localeCompare(subcomponentName) === 0).length;
  43.         if (item.component.localeCompare(componentName) === 0) {
  44.           if (dubSubComponent === 0) {
  45.             item.value.push(subcomponentName);
  46.           }
  47.         }
  48.       });
  49.  
  50.       const componentObj = {};
  51.  
  52.       componentObj.component = componentName;
  53.       componentObj.value = [];
  54.  
  55.       const dubComponent = el.value.filter((item) => item.component.localeCompare(componentName) === 0).length;
  56.       if (el.system.localeCompare(systemName) === 0) {
  57.         if (dubComponent === 0) {
  58.           el.value.push(componentObj);
  59.         }
  60.       }
  61.     });
  62.   });
  63.  
  64.   obj.sort((a, b) => a.value.length - b.value.length || a.system.localeCompare(b.sytem));
  65.   obj.reverse();
  66.  
  67.  
  68.   obj.forEach((element) => {
  69.     console.log(element.system);
  70.     element.value.sort((a, b) => b.value.length - a.value.length);
  71.     element.value.forEach((item) => {
  72.       console.log(`|||${item.component.trimStart()}`);
  73.       item.value.forEach((sub) => {
  74.         console.log(`||||||${sub.trimStart()}`);
  75.       });
  76.     });
  77.   });
  78. }
Add Comment
Please, Sign In to add comment