Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function printCommentsData(input) {
- let dataMap = new Map();
- let usersNames = [];
- let articleNames = [];
- for (let inputRow of input) {
- if (!inputRow.includes(":")) {
- if (inputRow.includes("user")) {
- let [user, username] = inputRow.split(' ');
- // if (!usersNames.includes(username)) {
- // }
- usersNames.push(username);
- } else if(inputRow.includes("article")) {
- let [article, articleName] = inputRow.split(' ');
- // if (!articleNames.includes(articleName)) {
- // }
- articleNames.push(articleName);
- }
- } else if (inputRow.includes(":")) {
- inputRow = inputRow.split(": ");
- inputRow[0] = inputRow[0].split(' ');
- let userName = inputRow[0].shift();
- let articleName = inputRow[0].pop();
- if (usersNames.includes(userName) && articleNames.includes(articleName)) {
- let [commentTitle, commentContent] = inputRow[1].split(', ');
- if (!dataMap.has(articleName)) {
- dataMap.set(articleName, new Map());
- dataMap.get(articleName).set(userName, new Map());
- dataMap.get(articleName).get(userName).set(commentTitle, commentContent);
- } else {
- dataMap.get(articleName).set(userName, new Map());
- dataMap.get(articleName).get(userName).set(commentTitle, commentContent);
- }
- }
- }
- }
- let arrFromDataMap = [...dataMap];
- for (let row of arrFromDataMap) {
- row[1] = [...row[1]];
- }
- let comparator = (a, b) => b[1].length - a[1].length;
- arrFromDataMap.sort(comparator);
- for (let row of arrFromDataMap) {
- // if (row[1].length > 1) {
- // }
- row[1].sort((a, b) => a[0].localeCompare(b[0]));
- }
- for (let row of arrFromDataMap) {
- console.log(`Comments on ${row[0]}`);
- for (let rowInner of row[1]) {
- console.log(`--- From user ${rowInner[0]}: ${[...rowInner[1].keys()][0]} - ${[...rowInner[1].values()][0]}`);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement