How to Grab information from an XMLHttp request and convert into an array.











up vote
0
down vote

favorite












I am currently working on an app and have gotten stuck. I have created a XMLHttp request and need to pull the information from it and put it into an array so I can create markers out of the request on a google map. The code is:



import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';


//"https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
const Http = new XMLHttpRequest();
const url = "https://api.foursquare.com/v2/venues/search? ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
Http.open('GET', url)
Http.send();

Http.onreadystatechange=(e)=>{
console.log(Http.responseText)
}



var AllPlaces = [



]

class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
filteredPlaces:
};

markers =


onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
}

onLiClick = (i) =>{
this.setState({
showingInfoWindow: true,
activeMarker: this.markers[i],
selectedPlace: AllPlaces[i]
})
}


onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
}


CreateInputField = () => (
<input
placeholder = "Search Nearby Places"
onChange={(event) => this.setState({filteredPlaces: AllPlaces.filter(place
=> !place.name.startsWith((event.target.value).toLowerCase()))})}
/>
)

render() {
return (
<div className = 'map-container' role='application' style=
{{marginleft:'250px'}}>
<div>
<div className = 'navMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
!this.state.filteredPlaces.includes(arrayItem) &&
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
!this.state.filteredPlaces.includes(marker) &&
<Marker
ref={(e) => {if (e) this.markers[i] =
e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
key = {i}
name={marker.name}
position =
{{lat:marker.lat,lng:marker.lng}}
/>
)}
<InfoWindow
onOpen={this.windowHasOpened}
onClose={this.windowHasClosed}
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
);
}
}

export default GoogleApiWrapper({
apiKey: 'AIzaSyD2BmXOGrCZQiAg5HJ6hU70BXc5v6Osr6M'
})(MapContainer)


I need to take in information I get from the XML request and put each object into the array I have at the top called AllPlaces I have tried with response and response text but have no idea how to loop through the JSON object that is returned.










share|improve this question


















  • 1




    Make sure you remove your API Key before sharing with other people :)
    – squeekyDave
    Nov 21 at 15:53










  • You could do a foreach loop through the response object. And then manually push each item into your array.
    – Anthony Sobo
    Nov 21 at 16:40










  • @AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
    – Hyunjin Kim
    Nov 21 at 16:44










  • When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
    – Anthony Sobo
    Nov 21 at 16:48










  • @AnthonySobo No, nothing gets logged to the console when I try to loop over it
    – Hyunjin Kim
    Nov 21 at 16:59















up vote
0
down vote

favorite












I am currently working on an app and have gotten stuck. I have created a XMLHttp request and need to pull the information from it and put it into an array so I can create markers out of the request on a google map. The code is:



import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';


//"https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
const Http = new XMLHttpRequest();
const url = "https://api.foursquare.com/v2/venues/search? ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
Http.open('GET', url)
Http.send();

Http.onreadystatechange=(e)=>{
console.log(Http.responseText)
}



var AllPlaces = [



]

class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
filteredPlaces:
};

markers =


onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
}

onLiClick = (i) =>{
this.setState({
showingInfoWindow: true,
activeMarker: this.markers[i],
selectedPlace: AllPlaces[i]
})
}


onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
}


CreateInputField = () => (
<input
placeholder = "Search Nearby Places"
onChange={(event) => this.setState({filteredPlaces: AllPlaces.filter(place
=> !place.name.startsWith((event.target.value).toLowerCase()))})}
/>
)

render() {
return (
<div className = 'map-container' role='application' style=
{{marginleft:'250px'}}>
<div>
<div className = 'navMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
!this.state.filteredPlaces.includes(arrayItem) &&
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
!this.state.filteredPlaces.includes(marker) &&
<Marker
ref={(e) => {if (e) this.markers[i] =
e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
key = {i}
name={marker.name}
position =
{{lat:marker.lat,lng:marker.lng}}
/>
)}
<InfoWindow
onOpen={this.windowHasOpened}
onClose={this.windowHasClosed}
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
);
}
}

export default GoogleApiWrapper({
apiKey: 'AIzaSyD2BmXOGrCZQiAg5HJ6hU70BXc5v6Osr6M'
})(MapContainer)


I need to take in information I get from the XML request and put each object into the array I have at the top called AllPlaces I have tried with response and response text but have no idea how to loop through the JSON object that is returned.










share|improve this question


















  • 1




    Make sure you remove your API Key before sharing with other people :)
    – squeekyDave
    Nov 21 at 15:53










  • You could do a foreach loop through the response object. And then manually push each item into your array.
    – Anthony Sobo
    Nov 21 at 16:40










  • @AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
    – Hyunjin Kim
    Nov 21 at 16:44










  • When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
    – Anthony Sobo
    Nov 21 at 16:48










  • @AnthonySobo No, nothing gets logged to the console when I try to loop over it
    – Hyunjin Kim
    Nov 21 at 16:59













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am currently working on an app and have gotten stuck. I have created a XMLHttp request and need to pull the information from it and put it into an array so I can create markers out of the request on a google map. The code is:



import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';


//"https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
const Http = new XMLHttpRequest();
const url = "https://api.foursquare.com/v2/venues/search? ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
Http.open('GET', url)
Http.send();

Http.onreadystatechange=(e)=>{
console.log(Http.responseText)
}



var AllPlaces = [



]

class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
filteredPlaces:
};

markers =


onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
}

onLiClick = (i) =>{
this.setState({
showingInfoWindow: true,
activeMarker: this.markers[i],
selectedPlace: AllPlaces[i]
})
}


onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
}


CreateInputField = () => (
<input
placeholder = "Search Nearby Places"
onChange={(event) => this.setState({filteredPlaces: AllPlaces.filter(place
=> !place.name.startsWith((event.target.value).toLowerCase()))})}
/>
)

render() {
return (
<div className = 'map-container' role='application' style=
{{marginleft:'250px'}}>
<div>
<div className = 'navMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
!this.state.filteredPlaces.includes(arrayItem) &&
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
!this.state.filteredPlaces.includes(marker) &&
<Marker
ref={(e) => {if (e) this.markers[i] =
e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
key = {i}
name={marker.name}
position =
{{lat:marker.lat,lng:marker.lng}}
/>
)}
<InfoWindow
onOpen={this.windowHasOpened}
onClose={this.windowHasClosed}
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
);
}
}

export default GoogleApiWrapper({
apiKey: 'AIzaSyD2BmXOGrCZQiAg5HJ6hU70BXc5v6Osr6M'
})(MapContainer)


I need to take in information I get from the XML request and put each object into the array I have at the top called AllPlaces I have tried with response and response text but have no idea how to loop through the JSON object that is returned.










share|improve this question













I am currently working on an app and have gotten stuck. I have created a XMLHttp request and need to pull the information from it and put it into an array so I can create markers out of the request on a google map. The code is:



import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';


//"https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
const Http = new XMLHttpRequest();
const url = "https://api.foursquare.com/v2/venues/search? ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
Http.open('GET', url)
Http.send();

Http.onreadystatechange=(e)=>{
console.log(Http.responseText)
}



var AllPlaces = [



]

class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
filteredPlaces:
};

markers =


onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
}

onLiClick = (i) =>{
this.setState({
showingInfoWindow: true,
activeMarker: this.markers[i],
selectedPlace: AllPlaces[i]
})
}


onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
}


CreateInputField = () => (
<input
placeholder = "Search Nearby Places"
onChange={(event) => this.setState({filteredPlaces: AllPlaces.filter(place
=> !place.name.startsWith((event.target.value).toLowerCase()))})}
/>
)

render() {
return (
<div className = 'map-container' role='application' style=
{{marginleft:'250px'}}>
<div>
<div className = 'navMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
!this.state.filteredPlaces.includes(arrayItem) &&
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
!this.state.filteredPlaces.includes(marker) &&
<Marker
ref={(e) => {if (e) this.markers[i] =
e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
key = {i}
name={marker.name}
position =
{{lat:marker.lat,lng:marker.lng}}
/>
)}
<InfoWindow
onOpen={this.windowHasOpened}
onClose={this.windowHasClosed}
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
);
}
}

export default GoogleApiWrapper({
apiKey: 'AIzaSyD2BmXOGrCZQiAg5HJ6hU70BXc5v6Osr6M'
})(MapContainer)


I need to take in information I get from the XML request and put each object into the array I have at the top called AllPlaces I have tried with response and response text but have no idea how to loop through the JSON object that is returned.







javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 15:51









Hyunjin Kim

173




173








  • 1




    Make sure you remove your API Key before sharing with other people :)
    – squeekyDave
    Nov 21 at 15:53










  • You could do a foreach loop through the response object. And then manually push each item into your array.
    – Anthony Sobo
    Nov 21 at 16:40










  • @AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
    – Hyunjin Kim
    Nov 21 at 16:44










  • When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
    – Anthony Sobo
    Nov 21 at 16:48










  • @AnthonySobo No, nothing gets logged to the console when I try to loop over it
    – Hyunjin Kim
    Nov 21 at 16:59














  • 1




    Make sure you remove your API Key before sharing with other people :)
    – squeekyDave
    Nov 21 at 15:53










  • You could do a foreach loop through the response object. And then manually push each item into your array.
    – Anthony Sobo
    Nov 21 at 16:40










  • @AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
    – Hyunjin Kim
    Nov 21 at 16:44










  • When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
    – Anthony Sobo
    Nov 21 at 16:48










  • @AnthonySobo No, nothing gets logged to the console when I try to loop over it
    – Hyunjin Kim
    Nov 21 at 16:59








1




1




Make sure you remove your API Key before sharing with other people :)
– squeekyDave
Nov 21 at 15:53




Make sure you remove your API Key before sharing with other people :)
– squeekyDave
Nov 21 at 15:53












You could do a foreach loop through the response object. And then manually push each item into your array.
– Anthony Sobo
Nov 21 at 16:40




You could do a foreach loop through the response object. And then manually push each item into your array.
– Anthony Sobo
Nov 21 at 16:40












@AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
– Hyunjin Kim
Nov 21 at 16:44




@AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
– Hyunjin Kim
Nov 21 at 16:44












When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
– Anthony Sobo
Nov 21 at 16:48




When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
– Anthony Sobo
Nov 21 at 16:48












@AnthonySobo No, nothing gets logged to the console when I try to loop over it
– Hyunjin Kim
Nov 21 at 16:59




@AnthonySobo No, nothing gets logged to the console when I try to loop over it
– Hyunjin Kim
Nov 21 at 16:59

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415789%2fhow-to-grab-information-from-an-xmlhttp-request-and-convert-into-an-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415789%2fhow-to-grab-information-from-an-xmlhttp-request-and-convert-into-an-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...