Guest User

Untitled

a guest
Dec 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. mark johannson waffle iron 80 2
  2. mark johannson blender 200 1
  3. mark johannson knife 10 4
  4. Nikita Smith waffle iron 80 1
  5. Nikita Smith knife 10 2
  6. Nikita Smith pot 20 3
  7.  
  8. var fs = require("fs");
  9.  
  10. var output = fs
  11. .readFileSync("data.txt", "utf-8")
  12. .trim()
  13. // NOTE: On Windows, I needed to use rn. You may only need r or n.
  14. .split("rn")
  15. .map(line => line.split("t"))
  16. .reduce((customers, line) => {
  17. customers[line[0]] = customers[line[0]] || [];
  18. customers[line[0]].push({
  19. name: line[1],
  20. price: line[2],
  21. quantity: line[3]
  22. });
  23. return customers;
  24. }, {});
  25.  
  26. console.log("output", JSON.stringify(output, null, 2));
  27.  
  28. node reduce.js
  29.  
  30. {
  31. "mark johannson": [
  32. {
  33. "name": "waffle iron",
  34. "price": "80",
  35. "quantity": "2"
  36. },
  37. {
  38. "name": "blender",
  39. "price": "200",
  40. "quantity": "1"
  41. },
  42. {
  43. "name": "knife",
  44. "price": "10",
  45. "quantity": "4"
  46. }
  47. ],
  48. "Nikita Smith": [
  49. {
  50. "name": "waffle iron",
  51. "price": "80",
  52. "quantity": "1"
  53. },
  54. {
  55. "name": "knife",
  56. "price": "10",
  57. "quantity": "2"
  58. },
  59. {
  60. "name": "pot",
  61. "price": "20",
  62. "quantity": "3"
  63. }
  64. ]
  65. }
  66.  
  67. <Buffer 6d 61 72 6b 20 6a 6f 68 61 6e 6e 73 6f 6e 09 77 61 66 66 6c 65 20 69 72 6f 6e 09 38 30 09 32 0d 0a 6d 61 72 6b 20 6a 6f 68 61 6e 6e 73 6f 6e
  68. 09 62 6c ... >
  69.  
  70. mark johannson waffle iron 80 2
  71. mark johannson blender 200 1
  72. mark johannson knife 10 4
  73. Nikita Smith waffle iron 80 1
  74. Nikita Smith knife 10 2
  75. Nikita Smith pot 20 3
  76.  
  77. [ 'mark johannsontwaffle iront80t2',
  78. 'mark johannsontblendert200t1',
  79. 'mark johannsontknifet10t4',
  80. 'Nikita Smithtwaffle iront80t1',
  81. 'Nikita Smithtknifet10t2',
  82. 'Nikita Smithtpott20t3' ]
  83.  
  84. [ [ 'mark johannson', 'waffle iron', '80', '2' ],
  85. [ 'mark johannson', 'blender', '200', '1' ],
  86. [ 'mark johannson', 'knife', '10', '4' ],
  87. [ 'Nikita Smith', 'waffle iron', '80', '1' ],
  88. [ 'Nikita Smith', 'knife', '10', '2' ],
  89. [ 'Nikita Smith', 'pot', '20', '3' ] ]
  90.  
  91. .reduce((customers, line) => {
  92. customers[line[0]] = customers[line[0]] || [];
  93. customers[line[0]].push({
  94. name: line[1],
  95. price: line[2],
  96. quantity: line[3]
  97. });
  98. return customers;
  99. }, {});
  100.  
  101. customers[line[0]].push({
  102. name: line[1],
  103. price: line[2],
  104. quantity: line[3]
Add Comment
Please, Sign In to add comment