Guest User

Untitled

a guest
Jan 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. {
  2. 6:00AM: ['Get up', 'Make the bed', 'Brush my teeth'],
  3. 6:45AM: ['Take a shower'],
  4. 7:15AM: ['Have breakfast', 'Leave for school']
  5. }
  6.  
  7. [
  8. {
  9. "id": 1,
  10. "day": "Monday",
  11. "activities": "6:00AM - Get up, Make the bed, Brush my teeth. 6:45AM - Take a shower. 7:15AM - Have breakfast, Leave for school."
  12. },
  13. {
  14. "id": 2,
  15. "day": "Tuesday",
  16. "activities": "6:00AM - Get up, Make the bed, Brush my teeth. 6:45AM - Take a shower. 7:15AM - Have breakfast, Leave for school."
  17. }
  18. ]
  19.  
  20. let activities = [];
  21.  
  22. /* My function */
  23. private splitter() {
  24. // const activities holds just the array of strings
  25. const activities = this.itinerary.map(item => {
  26. return item.activities;
  27. });
  28.  
  29. let result = {}; // The object that will hold the transformed string for each loop
  30. const res = activities.map(str => {
  31. for (result of str.split('.').map(x => x.trim())) {
  32. if (result) {
  33. const [time, actions] = str.split(' - ');
  34. result[time] = actions.split(',').map(x => x.trim());
  35. }
  36. }
  37. return result;
  38. });
  39.  
  40. return res; // The final array with the transformed strings (now objects) as values
  41. }
  42.  
  43. [
  44. {
  45. 6:00AM: ['Get up', 'Make the bed', 'Brush my teeth'],
  46. 6:45AM: ['Take a shower'],
  47. 7:15AM: ['Have breakfast', 'Leave for school']
  48. },
  49. {
  50. 6:00AM: ['Get up', 'Make the bed', 'Brush my teeth'],
  51. 6:45AM: ['Take a shower'],
  52. 7:15AM: ['Have breakfast', 'Leave for school']
  53. }
  54. ]
  55.  
  56. Uncaught (in promise): TypeError: Cannot create property '6:00AM' on string '6:00AM - Get up, Make the bed, Brush my teeth.'
  57.  
  58. const str = "6:00AM - Get up, Make the bed, Brush my teeth. 6:45AM - Take a shower. 7:15AM - Have breakfast, Leave for school."
  59.  
  60. sentences = {};
  61. // split the string on periods, and trim each sentence
  62. for (sentence of str.split('.').map(x => x.trim())) {
  63. // you end up with a completely empty sentence when the last
  64. // sentence ends in a period, which is uninteresting
  65. if (sentence) {
  66. // split each sentence on a hyphen, and assume the first
  67. // element is time and the second is actions
  68. let [time, actions] = sentence.split(' - ');
  69.  
  70. // split the actions string on commas and trim whitespace;
  71. // store that in our sentences object
  72. sentences[time] = actions.split(',').map(x => x.trim());
  73. }
  74. }
  75.  
  76. { '6:00AM': [ 'Get up', 'Make the bed', 'Brush my teeth' ],
  77. '6:45AM': [ 'Take a shower' ],
  78. '7:15AM': [ 'Have breakfast', 'Leave for school' ] }
  79.  
  80. const string = "6:00AM - Get up, Make the bed, Brush my teeth. 6:45AM - Take a shower. 7:15AM - Have breakfast, Leave for school."
  81. const firstSplit = string.split(".");
  82.  
  83. const secondSplit = firstSplit.map(each => each.split(" - "))
  84.  
  85. const almostThere = secondSplit.reduce((object, subarray) => {
  86. object[subarray[0]] = subarray[1]
  87. return object
  88. }, {})
  89.  
  90. {
  91. 6:00AM: "Get up, Make the bed, Brush my teeth",
  92. 6:45AM: "Take a shower",
  93. 7:15AM: "Have breakfast, Leave for school"
  94. }
  95.  
  96. const yeay = secondSplit.reduce((object, subarray) => {
  97. object[subarray[0]] = subarray[1].split(", ")
  98. }, {})
  99.  
  100. const firstSplit = string.split(".")
  101. .map(each => each.split(" - "))
  102. .reduce((object, subarray) => {
  103. object[subarray[0]] = subarray[1].split(", ")
  104. return object
  105. }, {})
  106.  
  107. const yeay = string.split(".")
  108. .reduce((object, hourAndChores) => {
  109. const splittedHourAndChores = hourAndChores.split(" - ");
  110. object[splittedHourAndChores[0]] = splittedHourAndChores[1].split(", ")
  111. return object
  112. }, {})
Add Comment
Please, Sign In to add comment