Why am I getting two different results when reading a JSON file?

My first result:

My second result after I refresh the page with no changes:

My code:

function getJSON(path, callback) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() {
        if (httpRequest.readyState === 4) {
            if (httpRequest.status === 200) {
                var data = JSON.parse(httpRequest.responseText);
                if (callback) callback(data);
            }
        }
    };
    httpRequest.open('GET', path);
    httpRequest.send();
};



getJSON('dist/js/projects.json', function(data){
    let dataLength = Object.keys(data.projects).length;
    let pArr = [];

    for(let i = 0; i < dataLength; i++) {
        console.log('i: ' + i);
        pArr[0] = data.projects[i].pID;
        pArr[1] = data.projects[i].name;
        pArr[2] = data.projects[i].description;
        pArr[3] = data.projects[i].image;

        console.log(pArr);
    }


});

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.