When attempting to display a JSON array, a variable defined as numeric transforms into NaN

I'm attempting to split a JSON array into three-column Bootstrap rows using the code below. My approach involves increasing a numeric variable to 3, adding a new row div, and then setting it back to 1.

However, I am encountering an issue where the variable returns as "NaN" when I try to increase it for the first time.

I would greatly appreciate your assistance or any alternative ideas for splitting the JSON into 3-column rows.

Here is the code:

<script>
   $( document ).ready(function() {
    var coli;

    console.log( 'ready!'+coli );

   $.getJSON("games.json", function(data) {
        var html = '';
        var coli=1;
        $.each(data, function(key,value){
            if (coli==3) {
                html += '<div class="row">';
                console.log( "3!" );
            }

           html += '<div class="col-md-4 img-portfolio">';
           html += '<a href="portfolio-item.html">';
           html += '<img class="img-responsive img-hover" src="'+value.teaser+'" alt="">';
           html += '</a>';
           html += '<h3>';
           html += '<a href="portfolio-item.html">'+value.title+'</a>' ;
           html += '</h3>';
           html += '<p>'+coli+value.description+'</p>';
           html += '</div> ';


             if (coli==3) {
                html += '</div>';
                var coli=1;
                console.log( "1!" );
            }
            coli++;
            console.log( 'ready!'+coli );
        });

    $('#yourContainerId').html(html);
    });

      });
   </script> 

Thank you

Answer №1

attempt to substitute

if (coli==3) {
    html += '</div>';
    var coli=1;
    console.log( "1!" );
}

with

if (coli==3) {
    html += '</div>';
    coli=1;
    console.log( "1!" );
}

It is important not to redeclare the variable coli within the if statement block.

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

The anchorEl state in Material UI Popper is having trouble updating

I am currently facing an issue with the Material UI popper as the anchorEl state remains stuck at null. Although Material UI provides an example using a functional component, I am working with a class-based component where the logic is quite similar. I w ...

The delay in updating after a redirect in the Next.js router push operation is causing a minor delay

I am facing an issue in my NextJS application where there is a delay of about 3 seconds when redirecting the user back to the listing page after submitting a form that saves data to the database using react-hook-form and an API call. This delay is causing ...

Issue: Ajax is not defined

When attempting to call a URL in the background using the following code within a script tag: var request = new Ajax.Request(logoffURL, {method : 'post'}); I encountered a script error stating Ajax is undefined. Is it necessary to include any ...

Running Protractor tests can be frustratingly sluggish and frequently result in timeouts

After spending most of the afternoon struggling with this test, I've tried different approaches but none seem to work. The task at hand is searching for users within the company, generating a table, and selecting the user that matches the name. Curren ...

Fetching an image from Firebase Storage for integration into a Vue application

I am encountering an issue while trying to fetch an image from my firebase storage to display it in my Vue application. The upload process from the app to firebase storage runs smoothly, but there is an error during retrieval. My setup involves using the F ...

Is it possible to retrieve various nested json properties in kusto (KQL)?

I am receiving telemetry events sent to Playfab. Within these events, I need to extract the content of the Payload. Currently, I am able to retrieve all information from my event except for the SuperProperties nested within the Payload. The issue is that a ...

Streamlined conversion of JSON data to and from numerous small files

As I have a large list containing millions of small records in the form of dictionaries, my aim is to avoid serialized the entire list into a single JSON file. Instead, I am looking to write each record to its own separate file. When needed, I will then re ...

JavaScript file creation and opening issue in Firefox

Check out my code snippet below: var blob = new Blob([data], { type: 'text/plain' }); var downloadLink = angular.element('<a></a>'); downloadLink.attr('href', window.URL.createObjectURL(blob)); downloadLink.attr ...

Next-auth custom authentication provider with unique backend

I am currently experiencing an issue with sessions while using auth authentication. My next-auth version is 4.0.0-beta.4 (also tried beta.7 with the same results). My backend utilizes a custom JWT token system that returns an object containing an access t ...

Steer clear of using the array push method repeatedly

I have recently developed an object that contains an array. Within this array, I am pushing objects from JSON data. $scope.pagenumArr = {"attribute":[],"_name":"pagenum","__prefix":"xsl"}; if ($scope.pagenumArr.attribute.indexOf($scope.content ...

Lightbox.options does not exist as a function within the lightbox plugin

I recently incorporated a lightbox plugin into my website, which can be found at the following link: For displaying items on the page, I am using markup similar to this example: <a href="images/image-2.jpg" data-lightbox="my-image">Image #2</a&g ...

The webpage fails to return to its original position after the script has been executed

My website has a sticky div that stays at the top when scrolling down, but does not return to its original position when scrolling back up. Check out this example function fixDiv() { var $div = $("#navwrap"); if ($(window).scrollTop() > $div.data("top ...

What is the best way to generate an array from JSON data while ensuring that the values are not duplicated?

Upon receiving a JSON response from an API, the structure appears as follows: { "status": "success", "response": [ { "id": 1, "name": "SEA BUSES", "image": null }, { "id": 2, ...

The step-by-step guide to implementing async/await specifically for a 'for loop'

Is there a way to make 'submitToTheOthers' function run after 'let items = []' has completed, without needing an await within 'submitToTheOthers'? I am considering using await within the for loop in 'submitToTheOthers&apo ...

Leverage OpenID Connect in Azure Active Directory with authentication code flow

Currently, I am developing an authentication system for a NodeJS and Express web application that requires users to be directed to Microsoft SSO. To achieve this, I am utilizing passport-azure-ad and OpenID Connect. My main query is - Is it mandatory to ...

Unexpected issue with Typo3 v9 - receiving empty Ajax Plugin JSON response

Consider the following setup in TYPO3 for an ajax request: ajaxAutocomplte_page = PAGE ajaxAutocomplte_page { typeNum = 111871 10 = COA_INT 10 { userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run extensionNa ...

Efficiently update a multi-step form using Ajax and jQuery by adding new content without needing to reload the

Is it possible to create a multistep form on a single page without reloading the div with content from a PHP file, but instead appending it below? Here is my current progress: $(document).on('submit', '#reg-form', function(){ var ln = ...

Energetic flair for Vue animations

I am currently developing a VueJS sidebar component. The objective is to allow the parent to define a width and display a toggle button that smoothly slides the sidebar in and out. Here is an example: <template> <div class="sidebarContainer ...

After filtering the array in JavaScript, perform an additional process as a second step

My task involves manipulating an array through two methods in sequence: Filter the array Then, sort it The filter method I am using is as follows: filterArray(list){ return list.filter(item => !this.myCondition(item)); } The sort method I a ...

Incorporating a new textfield in Codeigniter through a button/link activation

Currently, I am working on designing a request form for my website. I am facing an issue with creating a button that can dynamically add new input fields when clicked. Unfortunately, I am unsure of how to resolve this problem. Picture this: [ button ] A ...