簡體   English   中英

如何在 JavaScript 中將字符串拆分為數組?

[英]How can I split string to array in JavaScript?

我有這樣的字符串,我無法更改它,它來自遠程 API。

"[TimeStamp [choice=DateTime [date=Date [year=180, month=UNSPECIFIED, day=66, dayOfWeek=UNSPECIFIED], time=31:75:2.224]], TimeStamp [choice=DateTime [date=Date [year=148, month=UNSPECIFIED, day=96, dayOfWeek=THURSDAY], time=30:74:147.66]], TimeStamp [choice=DateTime [date=Date [year=141, month=UNSPECIFIED, day=240, dayOfWeek=UNSPECIFIED], time=1:168:105.70]]]"

我想拆分它,因為它將是 JSON 數組(例如,來自 API 的數組沒有引號)。 我希望我的 output 是:

[
 "TimeStamp [choice=DateTime [date=Date [year=180, month=UNSPECIFIED, day=66, dayOfWeek=UNSPECIFIED], 
 time=31:75:2.224]]",
 "TimeStamp [choice=DateTime [date=Date [year=148, month=UNSPECIFIED, day=96, dayOfWeek=THURSDAY], 
 time=30:74:147.66]]",
 "TimeStamp [choice=DateTime [date=Date [year=141, month=UNSPECIFIED, day=240, dayOfWeek=UNSPECIFIED], 
 time=1:168:105.70]]"
]

任何人都可以幫忙嗎? 非常感謝

首先,您想用從 1 到 length-1 的slice去除周圍的[] 然后用更容易分割的標記字符替換要分割的位置,以便保留]T (我使用過-但它可以是任何唯一字符)。 然后分裂那個獨特的角色。

 let result='[TimeStamp [choice=DateTime [date=Date [year=180, month=UNSPECIFIED, day=66, dayOfWeek=UNSPECIFIED], time=31:75:2.224]], TimeStamp [choice=DateTime [date=Date [year=148, month=UNSPECIFIED, day=96, dayOfWeek=THURSDAY], time=30:74:147.66]], TimeStamp [choice=DateTime [date=Date [year=141, month=UNSPECIFIED, day=240, dayOfWeek=UNSPECIFIED], time=1:168:105.70]]]' result =result.slice(1,result.length-1).replace(/\], T/,']-T').split('-') console.log(result)

這是一個可怕的 API 響應。 但這里有一種處理那個特定的方法。

您需要檢索的信息是:

  • 星期幾
  • 時間

您可以使用String.prototype.match()檢索單個 arrays 中的每個信息...然后使用forEach循環構建對象數組...

 let APIresponse = "[TimeStamp [choice=DateTime [date=Date [year=180, month=UNSPECIFIED, day=66, dayOfWeek=UNSPECIFIED], time=31:75:2.224]], TimeStamp [choice=DateTime [date=Date [year=148, month=UNSPECIFIED, day=96, dayOfWeek=THURSDAY], time=30:74:147.66]], TimeStamp [choice=DateTime [date=Date [year=141, month=UNSPECIFIED, day=240, dayOfWeek=UNSPECIFIED], time=1:168:105.70]]]"; function weirdAPItoJSON(response) { // Count how many occurance of "TimeStamp" let occurances = response.match(/TimeStamp/g) console.log(`${occurances.length} timeStamps... Let's retreive all parts.`) // ======= let years = response.match(/year=\d{1,3}/g) console.log(years) // ======= let months = response.match(/month=[az AZ]{1,}/g) console.log(months) // ======= let days = response.match(/day=\d{1,3}/g) console.log(days) // ======= let daysOfWeek = response.match(/dayOfWeek=[az AZ]{1,}/g) console.log(months) // ======= let times = response.match(/time=\d{1,}:\d{1,}:\d{1,}\.\d{1,}/g) console.log(times) // ============= Build the resulting array of objects let result = [] occurances.forEach(function(item, index){ result[index] = {} result[index].year = parseInt(years[index].split("=")[1]) result[index].month = months[index].split("=")[1] result[index].day = parseInt(days[index].split("=")[1]) result[index].dayOfWeek = daysOfWeek[index].split("=")[1] result[index].time = times[index].split("=")[1] }) return result } let result = weirdAPItoJSON(APIresponse) console.log("\nResult:") console.log(JSON.stringify(result))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM