Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (arr) {
- let arr2 = [];
- let obj = {};
- let objArr = [];
- let objArr2 = [];
- let newArr = 1;
- let arr3 = [];
- for (let el of arr) {
- arr2.push(el.split(' | '));
- }
- arr2.sort((a, b) => a[0].localeCompare(b[0]));
- // console.log(arr2);
- for (let el of arr2) {
- for (let i = 0; i < el.length; i++) {
- let name = el[0];
- if (el[0] === objArr[0] || newArr === 1) {
- if (i === el.length - 1) {
- newArr = 0;
- }
- if (i === 0 && newArr === 1) {
- objArr.push(name);
- } else {
- if (el[i] !== name) {
- objArr.push(el[i]);
- }
- }
- } else {
- objArr2.push(objArr);
- objArr = [];
- newArr++;
- i = -1;
- }
- }
- if (el === arr2[arr2.length - 1]) {
- objArr2.push(objArr);
- }
- }
- // console.log(objArr2);
- for (let i = 0; i < objArr2.length; i++) {
- for (let j = 0; j < objArr2[i].length; j = j + 2) {
- let comp = objArr2[i][j - 1];
- let subComp = objArr2[i][j];
- if (j === 0) {
- obj.system = objArr2[i][j];
- } else {
- if (obj.hasOwnProperty(comp)) {
- obj[comp].push(subComp);
- } else {
- obj[comp] = [subComp];
- }
- }
- }
- arr3.push(obj);
- obj = {};
- }
- // console.log(objArr2);
- arr3.sort((a, b) => Object.keys(b).length === Object.keys(a).length ? a['system'].localeCompare(b['system']) : Object.keys(b).length - Object.keys(a).length);
- for (let i = 0; i < arr3.length; i++) {
- for (let key in arr3[i]) {
- let systemKey = arr3[i][key];
- if (key === 'system') {
- console.log(`${systemKey}`);
- }
- if (key !== 'system') {
- console.log(`|||${key}`);
- for (let j = 0; j < arr3[i][key].length; j++) {
- console.log(`||||||${arr3[i][key][j]}`);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement