[英]How to reduce a string into a custom object using several delimiters, Uncaught (in promise): TypeError: Cannot read property of undefined
我有一個以下格式的字符串...
“ 6:00 AM-起床,鋪床,刷牙。6:45 AM-洗澡。7:15 AM-吃早餐,上學。”
我想根據句號,逗號和破折號來減少該字符串。 基本上得到一個具有時間和活動的對象。 對象應該看起來像這樣...
{
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."
}
]
我想遍歷該數組,並用轉換后的字符串(即返回的對象)替換每個activities
屬性的值。 因此,我創建了一個單獨的變量,稱為activity,並將其實例化為一個數組,在該數組中我希望存儲從轉換activity property
返回的對象。 所以..
let activities = [];
/* My function */
private splitter() {
const activities = this.itinerary.map(item => {
return item.activities;
});
const results = {};
const res = activities.map(str => {
for (const result of str.split('.').map(x => x.trim())) {
if (result) {
const [time, actions] = result.split(' - ');
results[time] = actions.split(',').map(x => x.trim());
}
}
return results;
});
return res;
}
我希望能達到這樣的目標...
[
{
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.'
我該如何工作?
這將使用僅可在任何最新版本的Node.JS中運行的內置JavaScript來解決問題:
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());
}
}
然后最后的console.log(sentences)
為您提供:
{ '6:00AM': [ 'Get up', 'Make the bed', 'Brush my teeth' ],
'6:45AM': [ 'Take a shower' ],
'7:15AM': [ 'Have breakfast', 'Leave for school' ] }
也許這是一個非常幼稚的解決方案,但是您可以結合使用split
和map
來解決類似的問題。
假設您有字符串。 您可以開始使用第一個最大的分隔符(在此示例中為句點)對其進行分割。
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(".");
您現在在firstSplit
所擁有的內容類似於["6:00AM - Get up, Make the bed, Brush my teeth", "6:45AM - Take a shower", "7:15AM - Have breakfast, Leave for school"]
firstSplit
["6:00AM - Get up, Make the bed, Brush my teeth", "6:45AM - Take a shower", "7:15AM - Have breakfast, Leave for school"]
。 您現在可以做的是將該數組中的每個值進一步拆分為小時並激活。
由於我想拆分數組中的每個項目(使用結果獲取一個新數組),因此我將使用一個map
const secondSplit = firstSplit.map(each => each.split(" - "))
現在, secondSplit
看起來像[["6:00AM", "Get up, Make the bed, Brush my teeth"], ["6:45AM", "Take a shower"], ["7:15AM", "Have breakfast, Leave for school"]]
現在,將這個奇怪的數組數組轉換為一個對象,其中每個小數組的第一個位置是鍵,第二個是值。 我將使用vainilla javascript,但是當然可以使用任何js庫(例如lodash或ramda)來簡化
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"
}
注意,我們在每個對象值上都缺少一個拆分。 我們可以通過修改以前完成的reduce
來解決
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
}, {})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.