Advertisement
Guest User

Untitled

a guest
Jan 13th, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. function printCommentsData(input) {
  2. let dataMap = new Map();
  3. let usersNames = [];
  4. let articleNames = [];
  5.  
  6. for (let inputRow of input) {
  7. if (!inputRow.includes(":")) {
  8. if (inputRow.includes("user")) {
  9. let [user, username] = inputRow.split(' ');
  10. // if (!usersNames.includes(username)) {
  11. // }
  12. usersNames.push(username);
  13. } else if(inputRow.includes("article")) {
  14. let [article, articleName] = inputRow.split(' ');
  15. // if (!articleNames.includes(articleName)) {
  16. // }
  17. articleNames.push(articleName);
  18. }
  19.  
  20. } else if (inputRow.includes(":")) {
  21. inputRow = inputRow.split(": ");
  22. inputRow[0] = inputRow[0].split(' ');
  23. let userName = inputRow[0].shift();
  24. let articleName = inputRow[0].pop();
  25. if (usersNames.includes(userName) && articleNames.includes(articleName)) {
  26. let [commentTitle, commentContent] = inputRow[1].split(', ');
  27. if (!dataMap.has(articleName)) {
  28. dataMap.set(articleName, new Map());
  29. dataMap.get(articleName).set(userName, new Map());
  30. dataMap.get(articleName).get(userName).set(commentTitle, commentContent);
  31. } else {
  32. dataMap.get(articleName).set(userName, new Map());
  33. dataMap.get(articleName).get(userName).set(commentTitle, commentContent);
  34. }
  35. }
  36. }
  37. }
  38. let arrFromDataMap = [...dataMap];
  39. for (let row of arrFromDataMap) {
  40. row[1] = [...row[1]];
  41. }
  42. let comparator = (a, b) => b[1].length - a[1].length;
  43. arrFromDataMap.sort(comparator);
  44. for (let row of arrFromDataMap) {
  45. // if (row[1].length > 1) {
  46. // }
  47. row[1].sort((a, b) => a[0].localeCompare(b[0]));
  48.  
  49. }
  50. for (let row of arrFromDataMap) {
  51. console.log(`Comments on ${row[0]}`);
  52. for (let rowInner of row[1]) {
  53. console.log(`--- From user ${rowInner[0]}: ${[...rowInner[1].keys()][0]} - ${[...rowInner[1].values()][0]}`);
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement