我有一個文本訊息,其中我必須根據時間格式化值,然后將其保存到 excel 檔案中。我嘗試使用溢位和加入功能,但它有新行,所以它不起作用。有沒有辦法在 javascript 中做到這一點?
正文:
16:15 test1 success
17:05 test4 success
17:40 test4234 down
18:25 test322 success
19:10 test423 success
20:20 test123 down
21:05 test454 success
21:40 test4 success
22:40 test145 success
23:35 test1123 down
00:35 testxx success
20:20 test123 down
14:20 test126 down
13:20 test177 success
我的代碼.js
const data =
'16:15 EURJPY-OTC PUT
17:05 USDJPY-OTC PUT
17:40 EURJPY-OTC PUT
18:25 EURGBP-OTC CALL
19:10 USDJPY-OTC PUT
20:20 USDJPY-OTC PUT
21:05 NZDUSD-OTC CALL
21:40 EURUSD-OTC CALL
22:40 USDJPY-OTC PUT
23:35 USDJPY-OTC PUT
00:35 NZDUSD-OTC CALL';
console.log(data.split('').join(','),'datataaaaa')
uj5u.com熱心網友回復:
假設接收到的資料具有用于分隔條目和分隔欄位(每個條目)的相同分隔符,您仍然可以可靠地決議輸入,因為您知道每個條目始終具有 3 個欄位。
function parseEntries(data) {
const entries = []
const tokens = data.split(' ') // space is the delimiter
for (let i = 2; i < tokens.length; i = 3) {
entries.push({
time: tokens[i - 2],
name: tokens[i - 1],
status: tokens[i]
})
}
return entries
}
const data =
'16:15 EURJPY-OTC PUT 17:05 USDJPY-OTC PUT 17:40 EURJPY-OTC PUT 18:25 EURGBP-OTC CALL'
const entries = parseEntries(data)
console.log(entries)
entries.forEach(e => {
// do whatever you need here with each entry
// e.time
// e.name
// e.status
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/405660.html
標籤:
