I'm unable to access value's from a state array filled with objects.
I got the values when I stored them in an array, but when I try to console.log
the state, it prints out: [Object Object]
. I have tried the following to get the value, example:
var data=[{"coordinate":{"longitude":73.8679075241089,"latitude":18.515422222637756},"key":0,"color":"#ffc130"}];
console.log("data"+data[0].coordinate.longitude);
This is how I tried to get the value in React Native:
console.log("location "+JSON.stringify(this.state.markers));
var data=JSON.stringify(this.state.markers);
console.log("datamap"+JSON.parse(data));
var dataparse = JSON.parse(data);
console.log("data parse "+this.state.markers[0].coordinate.latitude);
This is where I declared the array:
class DefaultMarkers extends React.Component {
constructor(props) {
super(props);
this.state = {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
},
markers: [],
};
}
I want to store the latitude and longitude in an array.
我使用JSON.parse()和JSON.stringify()进行解析
const data = JSON.parse(JSON.stringify(this.state.markers));
Change your console.log, separate two values with the comma instead concatenating them.
Change from
console.log("location "+JSON.stringify(this.state.markers));
To
console.log("location ", JSON.stringify(this.state.markers));
Hope you will be able to print the value.
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.