Converting JSON data into an array

I want to extract data from a Json URL and organize it into an array structured as shown below:

var locations = [ ['Bondi Beach', -30.890542, 151.274856], ['Coogee Beach', -33.923036, 151.259052], ['Cronulla Beach', -34.028249, 151.157507], ['Manly Beach', -33.80010128657071, 151.28747820854187], ['Maroubra Beach', -33.950198, 151.259302] ]

Here is the snippet of code I am working with... can you spot any errors?

Updated:

    var locations = new Array();
    $(document).ready(function(){
            $.getJSON('c.js', function(jsonData) {
                    $.each(jsonData, function(key, value) {                                
                            $('ul').append('<li id="' + key + '">' + value.city + ' : ' + value.latitude + ' , ' + value.longitude +'</li>');
                            locations[key] = [value.city, value.latitude, value.longitude];
                    });
            });
            window.alert (locations);
    });

Solved

    var locations = new Array();
    $(document).ready(function(){
            $.getJSON('http://www.xxxxxx/index.php', function(jsonData) {
                    $.each(jsonData, function(key, value) {                                
                            $('ul').append('<li id="' + key + '">' + value.city + ' : ' + value.latitude + ' , ' + value.longitude +'</li>');
                            locations.push([value.city, value.latitude, value.longitude]);
                    });

But I discovered that all my code needed to be contained within this function for it to work properly.

Answer №1

It is impossible to fetch JSON data from

http://www.jucees.es.gov.br/api/v1/estatisticaConsultas/index.php
using an ajax request due to the lack of the required Access-Control-Allow-Origin header in the response.

Furthermore, there is a syntax error in

{ val.city, val.latitude, val.longitude }
. The correct format should be changed to
[ val.city, val.latitude, val.longitude ]
.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for accessing the content within a DIV tag in a new browser tab

$('.menu div.profile-btn').on('click', function () { $('.mainservice-page').fadeIn(1200); } The script above effectively displays the contents of the .mainservice-page div, but I would like to open them in a new tab. Is ...

The error message "ReferenceError: express is not defined" indicates that Node.js is

Recently, I started using Nodejs to develop web servers, utilizing the express module. To install it, I used the command: "sudo npm install -g express". However, upon running the program, an error occurred: "ReferenceError: express is not defined ...

What is the process for consumers to provide constructor parameters in Angular 2?

Is it possible to modify the field of a component instance? Let's consider an example in test.component.ts: @Component({ selector: 'test', }) export class TestComponent { @Input() temp; temp2; constructor(arg) { ...

Is it possible to manipulate a modal within a controller by utilizing a certain attribute in HTML, based on specific conditions (without relying on any bootstrap services), using AngularJS?

Within my application, I have a modal that is triggered by clicking on a button (ng-click) based on certain conditions. Here is the HTML code: <button type="button" class="btn btn-outlined" ng-click="vm.change()" data-modal-target="#add-save-all-alert ...

Is the final element of a multidimensional array in C sometimes unexpectedly printed by printf, based on the input?

I've been exploring the world of multidimensional arrays in C, and I'm finding myself perplexed by the unexpected behavior of printf() in the code snippet below. The purpose of this program is to initialize a 5x2 array, prompt the user for 5 int ...

What is the best way to hide or show child elements within a tree structure using a toggle function

My HTML code needs to be updated to include a toggle span and JavaScript functionality. This will allow the child elements to be hidden and only displayed when the parent is clicked. I am a beginner with JavaScript and would appreciate any help in resolv ...

The JS Uri request is being mishandled

My current challenge involves POSTing to a server using Request JS, specifically with some difficulties related to the path. return await request.get({ method: 'POST', uri: `${domain}/info/test/`, body: bodyAsString, head ...

Limiting the DatePicker in React JS to only display the current year: Tips and Tricks!

I'm currently implementing the KeyboardDatePicker component in my React application to allow users to choose a travel date. However, I am looking to restrict the date selection to only the current year. This means that users should not be able to pick ...

Creating interactive tables in JavaScript with dynamic row adding, editing and deleting capabilities

Whenever I try to add a new row by clicking the Add Row button, an error occurs. My goal is to append a new 'tr' every time I click the add row button. Each 'td' should include a checkbox, first name, last name, email, mobile number, ed ...

The Power of Embedded Commitments

As I tackle a function that involves multiple layers of asynchronous actions nested within loops of more asynchronous actions, I've come to realize the importance of understanding promises. My current code, in its pre-promise state, can be simplified ...

Angular noticed a shift in the expression once it was verified

Whenever I try to invoke a service within the (change) action method, I encounter this issue: ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ng-untouched: true'. Cur ...

Configuring Google Chart LineChart settings by utilizing the series attribute

I am looking to modify the options for my line chart. However, when I define the options as shown below, the first series setting gets ignored and only the second series property is applied. var options = { title: 'Temperature Graph ( sampling ev ...

What is the method for creating an array that is multiplied by 1 and then adding up all of

Having a particular issue with creating an array of size 1*x and then summing up the digits within it. I currently have this code in place, but need some suggestions to improve it. Any insights are appreciated. Thank you. #include <stdio.h> #inclu ...

Implement dynamic routing by using route files for both "/" and "/:slug"

Currently, I am in the process of restructuring my node/express application and aiming to segregate my routes efficiently. The current obstacle I face is: I intend to have a homepage along with a distinct page for extensions that do not coincide with othe ...

ng-include once the application has finished loading

Currently, my server is using handlebars to generate the initial HTML page. I would like to include a ng-include in this page to dynamically update the client side. However, every time my application runs, it loads the page and the data-ng-include="templa ...

Learn how to toggle the menu list visibility by clicking on a component in Vue

I seem to be having an issue with closing a menu item in vue and vuetify2. Here is the code snippet that I have: <v-menu transition="slide-y-transition" bottom left offset-y nudge-bot ...

Exploring the possibilities of ReactJS and the sleek design of

How can I alter the background color of a RaisedButton without having the CSS class name appear in the autogenerated div? Here is the button: <RaisedButton className="access-login" label="Acceder" type="submit"/> This is the specified CSS: .acces ...

Switch between different table rows

I have here a table that is used for displaying menu and submenu items. It's a mix of PHP (to fetch the menu items and their respective submenus) and HTML. What I am trying to figure out is how to toggle the visibility of only the submenu items under ...

React: Transforming mongoDB date format into a more user-friendly date display

An entry saved in MongoDB contains a property called "createdAt" with a timestamp. Take a look at the example below: createdAt: 2021-10-26T12:24:33.433+00:00 If we consider this date to be today, how can I achieve the following outcomes?: Show this date ...

Encountering difficulty selecting a dropdown sub-menu using Selenium WebDriver

I'm currently working on automating a website with selenium webdriver. The issue I'm encountering is that when I try to click on a menu item, the submenu pops up (actually a misplaced dropdown, UI issue), and although I can locate the element of ...