Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- 6:00AM: ['Get up', 'Make the bed', 'Brush my teeth'],
- 6:45AM: ['Take a shower'],
- 7:15AM: ['Have breakfast', 'Leave for school']
- }
- [
- {
- "id": 1,
- "day": "Monday",
- "activities": "6:00AM - Get up, Make the bed, Brush my teeth. 6:45AM - Take a shower. 7:15AM - Have breakfast, Leave for school."
- },
- {
- "id": 2,
- "day": "Tuesday",
- "activities": "6:00AM - Get up, Make the bed, Brush my teeth. 6:45AM - Take a shower. 7:15AM - Have breakfast, Leave for school."
- }
- ]
- let activities = [];
- /* My function */
- private splitter() {
- // const activities holds just the array of strings
- const activities = this.itinerary.map(item => {
- return item.activities;
- });
- let result = {}; // The object that will hold the transformed string for each loop
- const res = activities.map(str => {
- for (result of str.split('.').map(x => x.trim())) {
- if (result) {
- const [time, actions] = str.split(' - ');
- result[time] = actions.split(',').map(x => x.trim());
- }
- }
- return result;
- });
- return res; // The final array with the transformed strings (now objects) as values
- }
- [
- {
- 6:00AM: ['Get up', 'Make the bed', 'Brush my teeth'],
- 6:45AM: ['Take a shower'],
- 7:15AM: ['Have breakfast', 'Leave for school']
- },
- {
- 6:00AM: ['Get up', 'Make the bed', 'Brush my teeth'],
- 6:45AM: ['Take a shower'],
- 7:15AM: ['Have breakfast', 'Leave for school']
- }
- ]
- Uncaught (in promise): TypeError: Cannot create property '6:00AM' on string '6:00AM - Get up, Make the bed, Brush my teeth.'
- const str = "6:00AM - Get up, Make the bed, Brush my teeth. 6:45AM - Take a shower. 7:15AM - Have breakfast, Leave for school."
- sentences = {};
- // split the string on periods, and trim each sentence
- for (sentence of str.split('.').map(x => x.trim())) {
- // you end up with a completely empty sentence when the last
- // sentence ends in a period, which is uninteresting
- if (sentence) {
- // split each sentence on a hyphen, and assume the first
- // element is time and the second is actions
- let [time, actions] = sentence.split(' - ');
- // split the actions string on commas and trim whitespace;
- // store that in our sentences object
- sentences[time] = actions.split(',').map(x => x.trim());
- }
- }
- { '6:00AM': [ 'Get up', 'Make the bed', 'Brush my teeth' ],
- '6:45AM': [ 'Take a shower' ],
- '7:15AM': [ 'Have breakfast', 'Leave for school' ] }
- const string = "6:00AM - Get up, Make the bed, Brush my teeth. 6:45AM - Take a shower. 7:15AM - Have breakfast, Leave for school."
- const firstSplit = string.split(".");
- const secondSplit = firstSplit.map(each => each.split(" - "))
- const almostThere = secondSplit.reduce((object, subarray) => {
- object[subarray[0]] = subarray[1]
- return object
- }, {})
- {
- 6:00AM: "Get up, Make the bed, Brush my teeth",
- 6:45AM: "Take a shower",
- 7:15AM: "Have breakfast, Leave for school"
- }
- const yeay = secondSplit.reduce((object, subarray) => {
- object[subarray[0]] = subarray[1].split(", ")
- }, {})
- const firstSplit = string.split(".")
- .map(each => each.split(" - "))
- .reduce((object, subarray) => {
- object[subarray[0]] = subarray[1].split(", ")
- return object
- }, {})
- const yeay = string.split(".")
- .reduce((object, hourAndChores) => {
- const splittedHourAndChores = hourAndChores.split(" - ");
- object[splittedHourAndChores[0]] = splittedHourAndChores[1].split(", ")
- return object
- }, {})
Add Comment
Please, Sign In to add comment