javascript - How to apply js .join() method to my array after every two values in the array -
i have array of lat longs:
latlong = [51.49, -0.05, 51.48, 0.16]
i want convert string in format:
latlon_string = "lat,lon lat,lon lat,lon" working example: latlon_string = "51.49,-0.05 51.48,0.16"
i tried using join() method apply space after every 2 values couldn't figure out how that. or there better solution? thanks
you can join array , replace every second comma space this:
latlong = [51.49, -0.05, 51.48, 0.16]; latlon_string = latlong.join(',').replace(/(,[^,]*),/g ,'$1 '); console.log(latlon_string); // 51.49,-0.05 51.48,0.16
Comments
Post a Comment