简体   繁体   中英

javascript array.push returns undefined

So I keep adding onto an array and push works but all that happens is I get ITERATIONS many undefined objects in the array. The object I keep trying to push works fine when i use it on its own but when i try to put it in an array i get undefined

function multiCalc() {
    primeList = [];
    for (var i = 0; i < (iterations * 2); i += 2) {
        isPrime = true;
        var itTest = bigInt(test).add(i);

        for (var j = bigInt(itTest.divide(2).add(1)); j.compare(2) == 1; j = j.minus(1)) {
            if ((test.mod(j)) == 0) {
                isPrime = false;
            }
        }
        primeList.push({
            "prime_number": {
                "testNumber": itTest.toString(),
                "isPrime": isPrime,
                "wasTested": true
            }
        });

    }
    sendPrime(primeList, multiUrl);
}

thanks in advance

edit here is the send prime function

 function sendPrime(PrimeData, path){ 
    if(stop == false){
    $.ajax({
        url: path,
        type: 'post',
        async: true,
        dataType: 'json',
        success: function (data) {
        test = bigInt(data.testNumber);
        multiCalc();
        // calc();
        },
        data: PrimeData
    });
}

Ok I figured it out. The array wasn't getting sent via ajax. I stringified the data and then added the contentType: 'application/json', line so that rails would know it was receiving json

function sendPrime(primeData, path){ 
    if(stop == false){
        console.log(primeData);
    $.ajax({
        url: path,
        type: 'post',
        async: true,
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
        test = bigInt(data.testNumber);
        console.log(test);
        multiCalc();
        // calc();
        },
        data: JSON.stringify(primeData)
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM