Iterate through appended json file
up vote
1
down vote
favorite
I have this code:
function heatMapRange()
var script1 = document.createElement('script');
script1.src = 'allCoords.js';
document.getElementsByTagName('head')[0].appendChild(script1);
which appends the allCoords.js file above:
allCoords_callback(
"coordinates": [
[50.1729677, 12.6692243, 580000],
[50.001168, 14.4270033, 2895000],
[50.6988037, 13.9384015, 945000],
[50.1218161, 14.4824004, 409900],
[49.470061, 17.0937597, 1499000],
[49.8509959, 18.5593087, 380000]
]
);
What I want is to iterate through this data with something like this:
function allCoords_callback(results1)
for (var i = 0; i < results1.coordinates.length; i++)
alert(results1.coordinates[i]);
Is it possible?
javascript html json
add a comment |
up vote
1
down vote
favorite
I have this code:
function heatMapRange()
var script1 = document.createElement('script');
script1.src = 'allCoords.js';
document.getElementsByTagName('head')[0].appendChild(script1);
which appends the allCoords.js file above:
allCoords_callback(
"coordinates": [
[50.1729677, 12.6692243, 580000],
[50.001168, 14.4270033, 2895000],
[50.6988037, 13.9384015, 945000],
[50.1218161, 14.4824004, 409900],
[49.470061, 17.0937597, 1499000],
[49.8509959, 18.5593087, 380000]
]
);
What I want is to iterate through this data with something like this:
function allCoords_callback(results1)
for (var i = 0; i < results1.coordinates.length; i++)
alert(results1.coordinates[i]);
Is it possible?
javascript html json
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have this code:
function heatMapRange()
var script1 = document.createElement('script');
script1.src = 'allCoords.js';
document.getElementsByTagName('head')[0].appendChild(script1);
which appends the allCoords.js file above:
allCoords_callback(
"coordinates": [
[50.1729677, 12.6692243, 580000],
[50.001168, 14.4270033, 2895000],
[50.6988037, 13.9384015, 945000],
[50.1218161, 14.4824004, 409900],
[49.470061, 17.0937597, 1499000],
[49.8509959, 18.5593087, 380000]
]
);
What I want is to iterate through this data with something like this:
function allCoords_callback(results1)
for (var i = 0; i < results1.coordinates.length; i++)
alert(results1.coordinates[i]);
Is it possible?
javascript html json
I have this code:
function heatMapRange()
var script1 = document.createElement('script');
script1.src = 'allCoords.js';
document.getElementsByTagName('head')[0].appendChild(script1);
which appends the allCoords.js file above:
allCoords_callback(
"coordinates": [
[50.1729677, 12.6692243, 580000],
[50.001168, 14.4270033, 2895000],
[50.6988037, 13.9384015, 945000],
[50.1218161, 14.4824004, 409900],
[49.470061, 17.0937597, 1499000],
[49.8509959, 18.5593087, 380000]
]
);
What I want is to iterate through this data with something like this:
function allCoords_callback(results1)
for (var i = 0; i < results1.coordinates.length; i++)
alert(results1.coordinates[i]);
Is it possible?
javascript html json
javascript html json
edited Nov 12 at 5:58
Busy Bee
9451619
9451619
asked Apr 3 '17 at 14:07
nocturne
2901519
2901519
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You can iterate an array in javascript with Array.map().
In your example will be something like:
results1.coordinates.map(function(coordinate) alert(coordinate); )
That's about iterating part.
Then, another topic is how do you get JSON you need to process. In the example given on Google Maps docs they do it using JSONP just because that is the way the real-time earthquake data works. Another method to fetch data are XMLHttpRequests (AKA as AJAX). This is a more common practice and I would recomend using it if possible.
In your case I would re-write your code to look something like this:
function heatMapRange()
var request = new XMLHttpRequest();
request.open('GET', '/allCoords.js', true);
request.onload = function ()
if (request.status >= 200 && request.status < 400)
// Success!
var data = JSON.parse(request.responseText);
// process the data in the response, like
// iterating through the list of coordinates
data.coordinates.map(function(coordinate) alert(coordinate); )
else
// We reached our target server, but it returned an error
request.error = function ()
// There was a connection error of some sort
request.send();
Which fetch the data from the JSON file allCoords.json
:
"coordinates": [
[50.1729677,12.6692243,580000],
[50.001168,14.4270033,2895000],
[50.6988037,13.9384015,945000],
[50.1218161,14.4824004,409900],
[49.470061,17.0937597,1499000],
[49.8509959,18.5593087,380000]
]
This way of fetching data from a server align more with the best practices used in the industry. This is just the straight forward example using vanillaJS XMLHttpRequest. There are tons of libraries that simplify this action. Even better there is Fetch API coming tackling the topic of fetching resources.
So I should put this line in to the allCoords_callback? Because the callback never runs anyway
– nocturne
Apr 3 '17 at 14:16
1
Ok, that is another question. I don't fully get why are you getting the coordinates object by appending a script instead of using XMLHttpRequest (AKA as AJAX). Can you give a bit of context on why you do that?
– Eloy Pineda
Apr 3 '17 at 14:23
I am using google maps API to create heatmaps using this example : developers.google.com/maps/documentation/javascript/earthquakes I was able to create heat map. My problem is that I need to add another child with json data to make another heatmap layer. I am doing the same thing as in this example but the callback never runs.
– nocturne
Apr 3 '17 at 14:28
1
Try to rename yourallCords_callback
founction toeqfeed_callback
and tell me what happens
– Eloy Pineda
Apr 3 '17 at 15:05
1
@nocturne, I have update the answer to cover the fetching data part of your question too, not just the iterating part. Please, let me know if this make sense to you.
– Eloy Pineda
Apr 5 '17 at 14:07
|
show 2 more comments
up vote
0
down vote
Well the code on the top works, problem was that I disabled the alerts in google chrome. So closing the tab and reopening page did the trick.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43186842%2fiterate-through-appended-json-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can iterate an array in javascript with Array.map().
In your example will be something like:
results1.coordinates.map(function(coordinate) alert(coordinate); )
That's about iterating part.
Then, another topic is how do you get JSON you need to process. In the example given on Google Maps docs they do it using JSONP just because that is the way the real-time earthquake data works. Another method to fetch data are XMLHttpRequests (AKA as AJAX). This is a more common practice and I would recomend using it if possible.
In your case I would re-write your code to look something like this:
function heatMapRange()
var request = new XMLHttpRequest();
request.open('GET', '/allCoords.js', true);
request.onload = function ()
if (request.status >= 200 && request.status < 400)
// Success!
var data = JSON.parse(request.responseText);
// process the data in the response, like
// iterating through the list of coordinates
data.coordinates.map(function(coordinate) alert(coordinate); )
else
// We reached our target server, but it returned an error
request.error = function ()
// There was a connection error of some sort
request.send();
Which fetch the data from the JSON file allCoords.json
:
"coordinates": [
[50.1729677,12.6692243,580000],
[50.001168,14.4270033,2895000],
[50.6988037,13.9384015,945000],
[50.1218161,14.4824004,409900],
[49.470061,17.0937597,1499000],
[49.8509959,18.5593087,380000]
]
This way of fetching data from a server align more with the best practices used in the industry. This is just the straight forward example using vanillaJS XMLHttpRequest. There are tons of libraries that simplify this action. Even better there is Fetch API coming tackling the topic of fetching resources.
So I should put this line in to the allCoords_callback? Because the callback never runs anyway
– nocturne
Apr 3 '17 at 14:16
1
Ok, that is another question. I don't fully get why are you getting the coordinates object by appending a script instead of using XMLHttpRequest (AKA as AJAX). Can you give a bit of context on why you do that?
– Eloy Pineda
Apr 3 '17 at 14:23
I am using google maps API to create heatmaps using this example : developers.google.com/maps/documentation/javascript/earthquakes I was able to create heat map. My problem is that I need to add another child with json data to make another heatmap layer. I am doing the same thing as in this example but the callback never runs.
– nocturne
Apr 3 '17 at 14:28
1
Try to rename yourallCords_callback
founction toeqfeed_callback
and tell me what happens
– Eloy Pineda
Apr 3 '17 at 15:05
1
@nocturne, I have update the answer to cover the fetching data part of your question too, not just the iterating part. Please, let me know if this make sense to you.
– Eloy Pineda
Apr 5 '17 at 14:07
|
show 2 more comments
up vote
2
down vote
accepted
You can iterate an array in javascript with Array.map().
In your example will be something like:
results1.coordinates.map(function(coordinate) alert(coordinate); )
That's about iterating part.
Then, another topic is how do you get JSON you need to process. In the example given on Google Maps docs they do it using JSONP just because that is the way the real-time earthquake data works. Another method to fetch data are XMLHttpRequests (AKA as AJAX). This is a more common practice and I would recomend using it if possible.
In your case I would re-write your code to look something like this:
function heatMapRange()
var request = new XMLHttpRequest();
request.open('GET', '/allCoords.js', true);
request.onload = function ()
if (request.status >= 200 && request.status < 400)
// Success!
var data = JSON.parse(request.responseText);
// process the data in the response, like
// iterating through the list of coordinates
data.coordinates.map(function(coordinate) alert(coordinate); )
else
// We reached our target server, but it returned an error
request.error = function ()
// There was a connection error of some sort
request.send();
Which fetch the data from the JSON file allCoords.json
:
"coordinates": [
[50.1729677,12.6692243,580000],
[50.001168,14.4270033,2895000],
[50.6988037,13.9384015,945000],
[50.1218161,14.4824004,409900],
[49.470061,17.0937597,1499000],
[49.8509959,18.5593087,380000]
]
This way of fetching data from a server align more with the best practices used in the industry. This is just the straight forward example using vanillaJS XMLHttpRequest. There are tons of libraries that simplify this action. Even better there is Fetch API coming tackling the topic of fetching resources.
So I should put this line in to the allCoords_callback? Because the callback never runs anyway
– nocturne
Apr 3 '17 at 14:16
1
Ok, that is another question. I don't fully get why are you getting the coordinates object by appending a script instead of using XMLHttpRequest (AKA as AJAX). Can you give a bit of context on why you do that?
– Eloy Pineda
Apr 3 '17 at 14:23
I am using google maps API to create heatmaps using this example : developers.google.com/maps/documentation/javascript/earthquakes I was able to create heat map. My problem is that I need to add another child with json data to make another heatmap layer. I am doing the same thing as in this example but the callback never runs.
– nocturne
Apr 3 '17 at 14:28
1
Try to rename yourallCords_callback
founction toeqfeed_callback
and tell me what happens
– Eloy Pineda
Apr 3 '17 at 15:05
1
@nocturne, I have update the answer to cover the fetching data part of your question too, not just the iterating part. Please, let me know if this make sense to you.
– Eloy Pineda
Apr 5 '17 at 14:07
|
show 2 more comments
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can iterate an array in javascript with Array.map().
In your example will be something like:
results1.coordinates.map(function(coordinate) alert(coordinate); )
That's about iterating part.
Then, another topic is how do you get JSON you need to process. In the example given on Google Maps docs they do it using JSONP just because that is the way the real-time earthquake data works. Another method to fetch data are XMLHttpRequests (AKA as AJAX). This is a more common practice and I would recomend using it if possible.
In your case I would re-write your code to look something like this:
function heatMapRange()
var request = new XMLHttpRequest();
request.open('GET', '/allCoords.js', true);
request.onload = function ()
if (request.status >= 200 && request.status < 400)
// Success!
var data = JSON.parse(request.responseText);
// process the data in the response, like
// iterating through the list of coordinates
data.coordinates.map(function(coordinate) alert(coordinate); )
else
// We reached our target server, but it returned an error
request.error = function ()
// There was a connection error of some sort
request.send();
Which fetch the data from the JSON file allCoords.json
:
"coordinates": [
[50.1729677,12.6692243,580000],
[50.001168,14.4270033,2895000],
[50.6988037,13.9384015,945000],
[50.1218161,14.4824004,409900],
[49.470061,17.0937597,1499000],
[49.8509959,18.5593087,380000]
]
This way of fetching data from a server align more with the best practices used in the industry. This is just the straight forward example using vanillaJS XMLHttpRequest. There are tons of libraries that simplify this action. Even better there is Fetch API coming tackling the topic of fetching resources.
You can iterate an array in javascript with Array.map().
In your example will be something like:
results1.coordinates.map(function(coordinate) alert(coordinate); )
That's about iterating part.
Then, another topic is how do you get JSON you need to process. In the example given on Google Maps docs they do it using JSONP just because that is the way the real-time earthquake data works. Another method to fetch data are XMLHttpRequests (AKA as AJAX). This is a more common practice and I would recomend using it if possible.
In your case I would re-write your code to look something like this:
function heatMapRange()
var request = new XMLHttpRequest();
request.open('GET', '/allCoords.js', true);
request.onload = function ()
if (request.status >= 200 && request.status < 400)
// Success!
var data = JSON.parse(request.responseText);
// process the data in the response, like
// iterating through the list of coordinates
data.coordinates.map(function(coordinate) alert(coordinate); )
else
// We reached our target server, but it returned an error
request.error = function ()
// There was a connection error of some sort
request.send();
Which fetch the data from the JSON file allCoords.json
:
"coordinates": [
[50.1729677,12.6692243,580000],
[50.001168,14.4270033,2895000],
[50.6988037,13.9384015,945000],
[50.1218161,14.4824004,409900],
[49.470061,17.0937597,1499000],
[49.8509959,18.5593087,380000]
]
This way of fetching data from a server align more with the best practices used in the industry. This is just the straight forward example using vanillaJS XMLHttpRequest. There are tons of libraries that simplify this action. Even better there is Fetch API coming tackling the topic of fetching resources.
edited Apr 8 '17 at 7:16
answered Apr 3 '17 at 14:13
Eloy Pineda
1,7771117
1,7771117
So I should put this line in to the allCoords_callback? Because the callback never runs anyway
– nocturne
Apr 3 '17 at 14:16
1
Ok, that is another question. I don't fully get why are you getting the coordinates object by appending a script instead of using XMLHttpRequest (AKA as AJAX). Can you give a bit of context on why you do that?
– Eloy Pineda
Apr 3 '17 at 14:23
I am using google maps API to create heatmaps using this example : developers.google.com/maps/documentation/javascript/earthquakes I was able to create heat map. My problem is that I need to add another child with json data to make another heatmap layer. I am doing the same thing as in this example but the callback never runs.
– nocturne
Apr 3 '17 at 14:28
1
Try to rename yourallCords_callback
founction toeqfeed_callback
and tell me what happens
– Eloy Pineda
Apr 3 '17 at 15:05
1
@nocturne, I have update the answer to cover the fetching data part of your question too, not just the iterating part. Please, let me know if this make sense to you.
– Eloy Pineda
Apr 5 '17 at 14:07
|
show 2 more comments
So I should put this line in to the allCoords_callback? Because the callback never runs anyway
– nocturne
Apr 3 '17 at 14:16
1
Ok, that is another question. I don't fully get why are you getting the coordinates object by appending a script instead of using XMLHttpRequest (AKA as AJAX). Can you give a bit of context on why you do that?
– Eloy Pineda
Apr 3 '17 at 14:23
I am using google maps API to create heatmaps using this example : developers.google.com/maps/documentation/javascript/earthquakes I was able to create heat map. My problem is that I need to add another child with json data to make another heatmap layer. I am doing the same thing as in this example but the callback never runs.
– nocturne
Apr 3 '17 at 14:28
1
Try to rename yourallCords_callback
founction toeqfeed_callback
and tell me what happens
– Eloy Pineda
Apr 3 '17 at 15:05
1
@nocturne, I have update the answer to cover the fetching data part of your question too, not just the iterating part. Please, let me know if this make sense to you.
– Eloy Pineda
Apr 5 '17 at 14:07
So I should put this line in to the allCoords_callback? Because the callback never runs anyway
– nocturne
Apr 3 '17 at 14:16
So I should put this line in to the allCoords_callback? Because the callback never runs anyway
– nocturne
Apr 3 '17 at 14:16
1
1
Ok, that is another question. I don't fully get why are you getting the coordinates object by appending a script instead of using XMLHttpRequest (AKA as AJAX). Can you give a bit of context on why you do that?
– Eloy Pineda
Apr 3 '17 at 14:23
Ok, that is another question. I don't fully get why are you getting the coordinates object by appending a script instead of using XMLHttpRequest (AKA as AJAX). Can you give a bit of context on why you do that?
– Eloy Pineda
Apr 3 '17 at 14:23
I am using google maps API to create heatmaps using this example : developers.google.com/maps/documentation/javascript/earthquakes I was able to create heat map. My problem is that I need to add another child with json data to make another heatmap layer. I am doing the same thing as in this example but the callback never runs.
– nocturne
Apr 3 '17 at 14:28
I am using google maps API to create heatmaps using this example : developers.google.com/maps/documentation/javascript/earthquakes I was able to create heat map. My problem is that I need to add another child with json data to make another heatmap layer. I am doing the same thing as in this example but the callback never runs.
– nocturne
Apr 3 '17 at 14:28
1
1
Try to rename your
allCords_callback
founction to eqfeed_callback
and tell me what happens– Eloy Pineda
Apr 3 '17 at 15:05
Try to rename your
allCords_callback
founction to eqfeed_callback
and tell me what happens– Eloy Pineda
Apr 3 '17 at 15:05
1
1
@nocturne, I have update the answer to cover the fetching data part of your question too, not just the iterating part. Please, let me know if this make sense to you.
– Eloy Pineda
Apr 5 '17 at 14:07
@nocturne, I have update the answer to cover the fetching data part of your question too, not just the iterating part. Please, let me know if this make sense to you.
– Eloy Pineda
Apr 5 '17 at 14:07
|
show 2 more comments
up vote
0
down vote
Well the code on the top works, problem was that I disabled the alerts in google chrome. So closing the tab and reopening page did the trick.
add a comment |
up vote
0
down vote
Well the code on the top works, problem was that I disabled the alerts in google chrome. So closing the tab and reopening page did the trick.
add a comment |
up vote
0
down vote
up vote
0
down vote
Well the code on the top works, problem was that I disabled the alerts in google chrome. So closing the tab and reopening page did the trick.
Well the code on the top works, problem was that I disabled the alerts in google chrome. So closing the tab and reopening page did the trick.
answered Apr 3 '17 at 16:38
nocturne
2901519
2901519
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43186842%2fiterate-through-appended-json-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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