简体   繁体   English

如何在 JavaScript 中将字符串拆分为数组?

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

I have the string like this, I am not able to change it, it's comming from remote API.我有这样的字符串,我无法更改它,它来自远程 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]]]"

I would like to split it as it would be JSON array (the one from API has no quotes for example).我想拆分它,因为它将是 JSON 数组(例如,来自 API 的数组没有引号)。 I want my output to be:我希望我的 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]]"
]

Anyone can help?任何人都可以帮忙吗? Thanks a lot非常感谢

First you want to get rid of the surrounding [] with a slice from 1 to length-1.首先,您想用从 1 到 length-1 的slice去除周围的[] Then replace where you want to split with an easier split demarking character so you can keep ] and T (I used - but it could be any unique character).然后用更容易分割的标记字符替换要分割的位置,以便保留]T (我使用过-但它可以是任何唯一字符)。 Then split on that unique character.然后分裂那个独特的角色。

 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)

This is an horrible API response.这是一个可怕的 API 响应。 But here a way to process that specific one.但这里有一种处理那个特定的方法。

The information you need to retreive are:您需要检索的信息是:

  • year
  • month
  • day
  • dayOfWeek星期几
  • time时间

You can use String.prototype.match() to retreive every information in individual arrays... Then use a forEach loop to build an array of objects...您可以使用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