这是我的代码:

var overallBlockList = Array();
    var blockListArray = Array();

$.each(data.blockList, function( key, value ) {
             blockListArray['blockNumber'] = value.blockNumber;
             blockListArray['age'] = value.age;
             blockListArray['txn'] = value.txn;
             blockListArray['uncles'] = value.uncles;
             blockListArray['miner'] = value.miner;
             blockListArray['gasUsed'] = value.gasUsed;
             blockListArray['gasLimit'] = value.gasLimit;
             blockListArray['avg.GasPrice'] = value['avg.GasPrice']['value'] + " " + value['avg.GasPrice']['unit'];
             blockListArray['reward'] = value['reward']['value'] + " " +value['reward']['unit'];

             overallBlockList[] = blockListArray; // In php, the overallBlockList Key will auto generate numeric. how about in Js syntax?
        });


我希望得到以下之一。 overallBlockList将具有带有blockListArray数据的数字键。但是overallBlockList[] = blockListArray;是php语法。 javascript语法如何才能生成totalBlockList的数字键?

[overallBlockList] => Array
                (
                    [0] => Array
                        (
                            [blockNumber] => 6764218
                            [age] => 1573608431
                            [txn] => 30
                            [uncles] => 0
                            [miner] => 0x4ccfb3039b78d3938588157564c9ad559bafab94
                            [gasUsed] => 3347881
                            [gasLimit] => 8000000
                            [avg.GasPrice] => 993239
                            [reward] => 39209320

                        )

                    [1] => Array
                        (
                            [blockNumber] => 6764217
                            [age] => 1573608410
                            [txn] => 54
                            [uncles] => 0
                            [miner] => 0x4ccfb3039b78d3938588157564c9ad559bafab94
                            [gasUsed] => 2300623
                            [gasLimit] => 8000000
                            [avg.GasPrice] => 329329
                            [reward] => 382938

                        )

最佳答案

arrays具有indicesobjects具有properties。在这里,您想要的是一组对象。

这就是代码的样子

var overallBlockList = [];

$.each(data.blockList, function(key, value) {
    var blockList = { };
    blockList['blockNumber'] = value.blockNumber;
    blockList['age'] = value.age;
    blockList['txn'] = value.txn;
    blockList['uncles'] = value.uncles;
    blockList['miner'] = value.miner;
    blockList['gasUsed'] = value.gasUsed;
    blockList['gasLimit'] = value.gasLimit;
    blockList['avg.GasPrice'] = value['avg.GasPrice']['value'] + " " + value['avg.GasPrice']['unit'];
    blockList['reward'] = value['reward']['value'] + " " + value['reward']['unit'];

    overallBlockList.push(blockList);
});


overallBlockList array将是

[
    {
        "blockNumber": 6764218,
        "age": 1573608431,
        "txn": 30,
        "uncles": 0,
        "miner": "0x4ccfb3039b78d3938588157564c9ad559bafab94",
        "gasUsed": 3347881,
        "gasLimit": 8000000,
        "avg.GasPrice": "993239 unit",
        "reward": "39209320 unit"
    },
    {
        "blockNumber": 6764217,
        "age": 1573608410,
        "txn": 54,
        "uncles": 0,
        "miner": "0x4ccfb3039b78d3938588157564c9ad559bafab94",
        "gasUsed": 2300623,
        "gasLimit": 8000000,
        "avg.GasPrice": "329329 unit",
        "reward": "382938 unit"
    }
]


代码可以简化为

var overallBlockList = [];

$.each(data.blockList, function(key, value) {
    overallBlockList.push({
        blockNumber: value.blockNumber,
        age: value.age,
        // ...
        'avg.GasPrice': value['avg.GasPrice'].value + " " + value['avg.GasPrice'].unit,
        reward: value.reward.value + " " + value.reward.unit
    });
});

10-04 19:34