Reputation: 11
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.
Upvotes: 0
Views: 1530
Reputation: 11
I used JSON.parse() and JSON.stringify() to parse it
const data = JSON.parse(JSON.stringify(this.state.markers));
Upvotes: 1
Reputation: 3142
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.
Upvotes: 0