response data in console I'm getting data mixed with latitude, longitude, altitude and speed in a single string. i want to segregate all 4 in 4 variables. I'm trying with index of but getting -1 in place of latitude, how to trim it properly
response data response data
Sample data
["13.8650839 78.5766843 867.699951171875 0.0"]
Expected output
latitude=13.8650839
longitude=78.5766843
altitude=867.699951171875
speed=0.0
code
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var responsedata = this.responseText;
const data = responsedata;
const [latitude, longitude, altitude, speed] = data[0].split(' ');
console.log(latitude, longitude, altitude, speed);
alert(latitude);
}
};
You can split the array at the spaces and assign them to variables like so, refer here to know more about destructuring
const data = '["12.8651929 77.576346 946.1199908085856 0.0"]'; const [latitude, longitude, altitude, speed] = JSON.parse(data)[0].split(' '); console.log(latitude, longitude, altitude, speed);
You can use split method in js to get the array like this
const str = ['13.8650839 78.5766843 867.699951171875 0.0'];
const words = str[0].split(' ')
console.log(words);
Now you get ouptut like this
Array ["13.8650839", "78.5766843", "867.699951171875", "0.0"]
Now you can extract easily you data using array index..
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.