What is the best way to show the contents of an array after making a getjson request?

My function makes two getJSON calls and writes the responses to an array. At the end of the second getJSON call, I used the code snippet below:

alert(files.length);
print_r(files);
console.log(files);

However, the alert shows the correct number of items in the array, but the print_r and console.log functions are not displaying the array items. I need to confirm that I have received the correct items in the array, but the print functionality is not working. Can anyone advise me on how to resolve this issue? (My ultimate goal is to sort the array, remove duplicates, and filter it further.)

    <script>
    var files = new Array();

       function pushtoArray(){

    //first getJSON call
    var url1 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json"; 
        $.getJSON(url1, function(data) {

          var entry = data.feed.entry;

          $(entry).each(function(){
            // Column names are name, age, etc.
        count++;
         files.push({ url: this.gsx$url.$t, filename: this.gsx$name.$t });

            $('.results').prepend('<h2>'+this.gsx$name.$t+'</h2><p>'+this.gsx$url.$t+'</p>');

          });

        alert(files.length);
        print_r(files);
        console.log(files);
        });//end of ajax call

    //second getJSON call
var url2 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json"; 
        $.getJSON(url2, function(data) {

          var entry = data.feed.entry;

          $(entry).each(function(){
            // Column names are name, age, etc.
        count++;
         files.push({ url: this.gsx$url.$t, filename: this.gsx$name.$t });

            $('.results').prepend('<h2>'+this.gsx$name.$t+'</h2><p>'+this.gsx$url.$t+'</p>');

          });

        alert(files.length);
        print_r(files);
        console.log(files);
        });//end of ajax call


        };//end of function

    </script>

HTML code:

<body onload="pushtoArray()">

Answer №1

In PHP, the print_r function is commonly used, whereas in JavaScript, console.log is the equivalent method for outputting information.

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

What is preventing me from navigating to other pages in my React application?

Recently, I have been experimenting with ReactJS and encountered an issue where I couldn't access my other pages. The code snippet provided below seems to be the root of the problem. I am in the process of developing a multi-page application using Re ...

Utilize Sorting while Keeping the Original Keys

Is there another method I can use instead of the sort function in php that won't delete all the keys? ...

Reading data from a text file and storing it in an array or list

I've been working on a project in Python where I need to extract values from a .txt file into an array or list. Let's assume that the data in my user.txt file looks like this: ghost:001 ghost:002 ghost:003 In order to output it as: &ap ...

Error: The method specified in $validator.methods[method] does not exist

Having trouble solving a problem, despite looking at examples and reading posts about the method. The error I'm encountering is: TypeError: $.validator.methods[method] is undefined Below that, it shows: result = $.validator.methods[method].call( t ...

Exploring the process of declaring an object within a JavaScript ES6 class constructor

How can I access constructor variables inside static methods in a class when the property is declared as 'static' with 'this' and not accessible? export class Reporter { constructor() { this.jsonReports = path.join(process. ...

Oops! Looks like there was a syntax error with the JSON data at position 1. This resulted in a 400

Having issues submitting form data in my Vue app with an express backend API. The endpoint works fine on postman but I keep getting errors like "SyntaxError: Unexpected token g in JSON at position 0" or "400: Bad Request." I've tried using JSON.parse ...

When utilizing an object within another object for a select option in Vue.js, the value may not update as expected

I'm currently working with an array of posts that includes a user for each post. I have a select field where I can change the user associated with a post. However, only the user's id updates on the page and not their username. Is there a way to e ...

"Creating multiple circles on an HTML5 canvas using an iPad: A step-by-step guide

My current code is only drawing one circle at a time on an iPad view, but I want to be able to draw multiple circles simultaneously. How can I achieve this? // Setting up touch events for drawing circles var canvas = document.getElementById('pain ...

What to do while waiting for MySQL query in an asynchronous function?

Having an issue with my asynchronous function that queries the database using the mysql library. It seems like the function is not waiting for the query to complete before moving on. Here's the code snippet: async (eventName, eventArgs, app) => { ...

Encountered an issue retrieving tweets from the Twitter API 1.1

I recently completed an online tutorial from this site: However, I'm encountering an error message that simply says 'error: ' without any additional information. To start, here is my PHP script used to fetch the JSON output: <?php sess ...

Angular's use of ES6 generator functions allows for easier management of

Recently, I have integrated generators into my Angular project. Here is how I have implemented it so far: function loadPosts(skip) { return $rootScope.spawn(function *() { try { let promise = yield User.findAll(); $time ...

Running JavaScript code when the route changes in Angular 6

Currently, I am in the process of upgrading a website that was originally developed using vanilla JS and JQuery to a new UI built with Angular and typescript. Our site also utilizes piwik for monitoring client activity, and the piwik module was created i ...

Perform a function on every element within a dynamic array

Imagine I have a struct struct point_2d { int x, int y }; In my software, I store an array of this type, main() { struct point_2d *coords = malloc(10*sizeof(struct point_2d)); ... } and I aim to perform an operation on each element (li ...

How can one overcome CORS policies to retrieve the title of a webpage using JavaScript?

As I work on a plugin for Obsidian that expands shortened urls like bit.ly or t.co to their full-length versions in Markdown, I encounter a problem. I need to fetch the page title in order to properly create a Markdown link [title](web link). Unfortunatel ...

Using JavaScript and jQuery, apply a class when a specific radio button is checked and the data attribute meets certain criteria

Need help with changing explanation color based on correct answer selection in multiple choice question. Currently, all choices turn green instead of just the selected correct one. Any assistance is welcome, thank you. html <li class='test-questi ...

Encountering errors while running Angular 8's ng build prod command

After successfully migrating my project from Angular 7 to Angular 8, I encountered an issue when attempting to run 'ng build prod' which resulted in the following error: ERROR in Error during template compile of 'Ng2CompleterModule' Cou ...

Allowing Cross-Origin Resource Sharing in WKWebView for IOS9

When trying to load a local HTML file on IOS using WKWebview with loadFileURL and allowingReadAccessToURL, I encounter an issue when sending ajax requests. An error message is displayed: "Origin null is not allowed by Access-Control-Allow-Origin." I a ...

Does anyone know if it's feasible to return a value from PHP to an HTML form without relying on JavaScript?

Currently, I am in the process of learning how to create a basic web form. My goal is to develop an HTML web form that sends a number to PHP and then displays the number in another text field without refreshing the page. After doing some research online, ...

Leveraging react props with the .map method

Imagine having this render function in one of your components. You've passed a changeTid prop function from the parent element. Parent Component: <RequestsList data={this.state.data} changeTid={this.changeTid} /> Child Component: (Using ES6 ...

Leveraging JSON for fetching distinct identifiers from a database

When a user hovers over a parent span containing an empty img tag, I use JSON and jQuery to retrieve the src of the image from the database. The issue arises when the retrieved src is applied to all looped span images on the same page, instead of each imag ...