gpt4 book ai didi

javascript - 如何在 JavaScript 中生成数组的数字键?

转载 作者:行者123 更新时间:2023-11-30 13:48:21 24 4
gpt4 key购买 nike

这是我的代码:

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 语法来生成 overallBlockList 的数字键?

[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

)

最佳答案

数组索引对象属性。在这里,您需要的是一个对象数组。

代码是这样的

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
});
});

关于javascript - 如何在 JavaScript 中生成数组的数字键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58830126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com