JSFiddle - React, Tailwind, and code Playground

by kmiklas

JavaScript

var pointsArray = new Array(9); // whatever length here

    // Populate array with some dummy data
    for (var i = pointsArray.length; i > -1; i--) {
        pointsArray[i] = 'row' + i;
    }

    console.log('~~~ Using immediate function with closure');
    console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
    for (var i = pointsArray.length - 1; i > -1; i--) {
        (function(j){
             /* AJAX call here... example here has dummy data. */
             var data_from_ajax_call = j + 42;
             console.log(pointsArray[j], data_from_ajax_call);
        })(i);
    }

    console.log('\n);
    console.log('~~~ Using forEach');
    console.log('~~~~~~~~~~~~~~~~~');

    var k = 0;
    pointsArray.forEach(function(rowName) {
        /* AJAX call here... example here has dummy data. */
        var data_from_ajax_call = k + 42;
        console.log(rowName, data_from_ajax_call);
        k +=1;    
    });
    console.log('\n\n');