split string using comma on number but not to decimal

In my database I have data like this

data.coordinat = 
[ 110.9147458709,-7.0832087668,45 // separator just make space
  110.9142635038,-7.0832160377,45
  110.9141638945,-7.0832160193,45
  110.9140642852,-7.0832160009,45
  110.9139556189,-7.0832250268,45
  110.9138469542,-7.0832250067,45
}

i’ve splite use data.coordinat.split(' ')//space and result :

0: "[110.9147458709,-7.0832087668,45]"
1: "[110.9142635038,-7.0832160377,45]"
2: "[110.9141638945,-7.0832160193,45]"
3: "[110.9140642852,-7.0832160009,45]"
4: "[110.9139556189,-7.0832250268,45]"
5: "[110.9138469542,-7.0832250067,45]"
6: "[110.9137382895,-7.0832249865,45]"
7: "[110.9136386785,-7.0832340140,45]"
8: "[110.9135390676,-7.0832430414,45]"

and now I am confused how to split with symbol comma , to get a result like this

[110.9147458709][-7.0832087668,45] // expect that result

>Solution :

Assuming each set is a string (they have to be or your array is invalid, as are your coordinates) we can replace the first comma and split on the replacement

const coordinates = [ 
"110.9147458709,-7.0832087668,45",
"110.9142635038,-7.0832160377,45",
"110.9141638945,-7.0832160193,45",
"110.9140642852,-7.0832160009,45",
"110.9139556189,-7.0832250268,45",
"110.9138469542,-7.0832250067,45"
]
const arr = coordinates.map(coor => {
  return coor.replace(",","#").split("#"); // replace will only replace the first comma
})
console.log(arr)

Leave a Reply