由于某种原因,此javascript似乎无法正确计算,并不断给我一些数组值的无穷大。

var stock = {
        "data": {
            "portfolio": [
                [1298641715000, 1631.00],
                [1298656616000, 1311.00],
                [1299606578000, 1311.00],
                [1300381305000, 820.50],
                [1300813418000, 2074.80],
                [1300970915000, 3862.50],
                [1300985378000, 4597.35],
                [1301071778000, 3554.85],
                [1301505492000, 5653.05],
                [1302195878000, 4871.80],
                [1302368766000, 4079.80],
                [1306343616000, 5062.60],
                [1307036495000, 5062.60],
                [1307726770000, 7207.60],
                [1307727695000, 6372.60],
                [1312998095000, 5816.10],
                [1313430095000, 5816.10],
                [1316625492000, 7816.10],
                [1316711892000, 6566.10],
                [1316713295000, 7777.10],
                [1316713295000, 8921.60],
                [1316799695000, 10902.10],
                [1316886095000, 8627.10],
                [1318094770000, 7717.10],
                [1318267163000, 6807.10],
                [1326306370000, 8228.30],
                [1327429500000, 9228.30],
                [1328208664000, 10222.05],
                [1329503831000, 10188.65],
                [1330368095000, 11274.15],
                [1330541464000, 12006.75],
                [1330714264000, 13222.25],
                [1331059408000, 14029.75],
                [1331144806000, 15676.55],
                [1333648264000, 17256.02],
                [1334080424000, 18701.62],
                [1336672860000, 19745.42],
                [1336758824000, 20018.77],
                [1336773969000, 21130.77],
                [1337015320000, 20312.47],
                [1337277827000, 21650.02],
                [1339002758000, 22285.97],
                [1341934145000, 23972.67],
                [1341934243000, 24182.67],
                [1342103859000, 25830.67],
                [1342475250000, 25380.67],
                [1342654950000, 25669.09],
                [1343147542000, 25797.43],
                [1343412475000, 25994.43],
                [1343658668000, 26203.43],
                [1343804523000, 26342.43],
                [1343840755000, 26432.43],
                [1343922704000, 25914.43],
                [1343922759000, 26031.43],
                [1345053816000, 26156.43],
                [1345138832000, 26253.43],
                [1345576479000, 26483.43],
                [1345576537000, 26588.43],
                [1345656708000, 26628.43],
                [1346442006000, 26704.43],
                [1346953652000, 26456.43],
                [1347553896000, 26586.43],
                [1347554286000, 26865.43],
                [1347559351000, 27062.43],
                [1347560040000, 27533.43],
                [1347560369000, 27351.43],
                [1347641968000, 26577.43],
                [1348063653000, 26860.43],
                [1348591198000, 26588.43],
                [1348591500000, 26598.43],
                [1348668767000, 26728.43],
                [1348681823000, 27094.43],
                [1349367062000, 27254.43],
                [1349368159000, 27379.43],
                [1350310748000, 27675.43],
                [1350503445000, 27858.43],
                [1350923279000, 27979.43],
                [1352131960000, 27742.43],
                [1353522221000, 27997.43],
                [1353685832000, 28299.43],
                [1355157961000, 28545.43],
                [1355766797000, 28692.43],
                [1355947837000, 28913.43],
                [1357671266000, 29304.43],
                [1357672054000, 29531.44],
                [1358448645000, 28856.44],
                [1358448738000, 29036.44],
                [1358455579000, 29236.44],
                [1358540610000, 29430.44],
                [1360095697000, 30077.44],
                [1360596277000, 30154.44],
                [1360612771000, 30651.44],
                [1360860220000, 30789.44],
                [1360869993000, 31389.44]
            ],
                "portfolio2": [
                [1298641715000, 1631.00],
                [1298656616000, 1311.00],
                [1299606578000, 1311.00],
                [1300381305000, 820.50],
                [1300813418000, 2074.80],
                [1300970915000, 3862.50],
                [1300985378000, 4597.35],
                [1301071778000, 3554.85],
                [1301505492000, 5653.05],
                [1302195878000, 4871.80],
                [1302368766000, 4079.80],
                [1306343616000, 5062.60],
                [1307036495000, 5062.60],
                [1307726770000, 7207.60],
                [1307727695000, 6372.60],
                [1312998095000, 5816.10],
                [1313430095000, 5816.10],
                [1316625492000, 7816.10],
                [1316711892000, 6566.10],
                [1316713295000, 7777.10],
                [1316713295000, 8921.60],
                [1316799695000, 10902.10],
                [1316886095000, 8627.10],
                [1318094770000, 7717.10],
                [1318267163000, 6807.10],
                [1326306370000, 8228.30],
                [1327429500000, 9228.30],
                [1328208664000, 10222.05],
                [1329503831000, 10188.65],
                [1330368095000, 11274.15],
                [1330541464000, 12006.75],
                [1330714264000, 13222.25],
                [1331059408000, 14029.75],
                [1331144806000, 15676.55],
                [1333648264000, 17256.02],
                [1334080424000, 18701.62],
                [1336672860000, 19745.42],
                [1336758824000, 20018.77],
                [1336773969000, 21130.77],
                [1337015320000, 20312.47],
                [1337277827000, 21650.02],
                [1339002758000, 22285.97],
                [1341934145000, 23972.67],
                [1341934243000, 24182.67],
                [1342103859000, 25830.67],
                [1342475250000, 25380.67],
                [1342654950000, 25669.09],
                [1343147542000, 25797.43],
                [1343412475000, 25994.43],
                [1343658668000, 26203.43],
                [1343804523000, 26342.43],
                [1343840755000, 26432.43],
                [1343922704000, 25914.43],
                [1343922759000, 26031.43],
                [1345053816000, 26156.43],
                [1345138832000, 26253.43],
                [1345576479000, 26483.43],
                [1345576537000, 26588.43],
                [1345656708000, 26628.43],
                [1346442006000, 26704.43],
                [1346953652000, 26456.43],
                [1347553896000, 26586.43],
                [1347554286000, 26865.43],
                [1347559351000, 27062.43],
                [1347560040000, 27533.43],
                [1347560369000, 27351.43],
                [1347641968000, 26577.43],
                [1348063653000, 26860.43],
                [1348591198000, 26588.43],
                [1348591500000, 26598.43],
                [1348668767000, 26728.43],
                [1348681823000, 27094.43],
                [1349367062000, 27254.43],
                [1349368159000, 27379.43],
                [1350310748000, 27675.43],
                [1350503445000, 27858.43],
                [1350923279000, 27979.43],
                [1352131960000, 27742.43],
                [1353522221000, 27997.43],
                [1353685832000, 28299.43],
                [1355157961000, 28545.43],
                [1355766797000, 28692.43],
                [1355947837000, 28913.43],
                [1357671266000, 29304.43],
                [1357672054000, 29531.44],
                [1358448645000, 28856.44],
                [1358448738000, 29036.44],
                [1358455579000, 29236.44],
                [1358540610000, 29430.44],
                [1360095697000, 30077.44],
                [1360596277000, 30154.44],
                [1360612771000, 30651.44],
                [1360860220000, 30789.44],
                [1360869993000, 31389.44]
            ],
                "standard": []
        }
    };

    var stockbase = stock.data.portfolio;
    var stock1 = stock.data.portfolio2;

    for (i = 0; i < (stock.data.portfolio2.length); i++) {
        stock.data.portfolio2[i][1] = stock.data.portfolio2[i][1] * Math.random();
    }

    //var performance1 = stock.data.portfolio2;

    var i = 0;

    while(i<stockbase.length)
    {
            if (i == 0){
            stock1[i][0] = stockbase[i][0];
            stock1[i][1] = 0;
        } else if (stockbase[i][1] > stockbase[(i - 1)][1]) {
            stock1[i][0] = stockbase[i][0];
            stock1[i][1] = (stockbase[i][1]) / (stockbase[(i - 1)][1]);
            stock1[i][1] -= 1;
            stock1[i][1] *= 100;
        } else if (stockbase[i][1] < stockbase[(i - 1)][1]) {
            stock1[i][0] = stockbase[i][0];
            stock1[i][1] = ((stockbase[i][1]) / (stockbase[(i-1)][1]));
            stock1[i][1] -= 1;
            stock1[i][1] *= 100;
        } else {
            stock1[i][0] = stockbase[i][0];
            stock1[i][1] = 0;
        }
        i++;
    }

    //console.log(stockbase);
    //console.log(stock1);

    var stockbase = stock.data.portfolio2;
    var stock2 = stock.data.portfolio;

    i = 0;

    while(i<stockbase.length)
    {
            if (i == 0){
            stock2[i][0] = stockbase[i][0];
            stock2[i][1] = 0;
        } else if (stockbase[i][1] > stockbase[(i - 1)][1]) {
            stock2[i][0] = stockbase[i][0];
            stock2[i][1] = (stockbase[i][1]) / (stockbase[(i - 1)][1]);
            stock2[i][1] -= 1;
            stock2[i][1] *= 100;
        } else if (stockbase[i][1] < stockbase[(i - 1)][1]) {
            stock2[i][0] = stockbase[i][0];
            stock2[i][1] = ((stockbase[i][1]) / (stockbase[(i-1)][1]));
            stock2[i][1] -= 1;
            stock2[i][1] *= 100;
        } else {
            stock2[i][0] = stockbase[i][0];
            stock2[i][1] = 0;
        }
        i++;
    }

    console.log(stockbase);
    console.log(stockbase==stock.data.portfolio2);


变量stock 1在循环后可以完美工作,没有任何问题。有趣的是,在第一个while循环之后,stock.data.portfolio2具有与stock1相同的数组值,即使不应对stock.data.portfolio2进行任何更改。

我对自己做错了感到困惑。

最佳答案

执行以下操作时:

var stock1 = stock.data.portfolio2;

您正在将stock1分配给数组stock.data.portfolio2的引用。这意味着对stock.data.portfolio2所做的所有更改也将在stock1中显示。由于stock.data.portfolio2是一个数组数组,因此我建议创建一个函数以将您的数据复制到变量stock1

function copyPortfolio(arr) {
    var s = [];
    for(i = 0; i < (arr.length); i++) { s.push(arr[i].slice()); }
    return s;
}

var stock1 = copyPortfolio(stock.data.portfolio2);

08-24 20:14