简体   繁体   中英

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.

"[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). I want my output to be:

[
 "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. 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). 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. 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...

 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM