JavaScript数组:是否可以同时拥有键和值?

huangapple go评论69阅读模式
英文:

Javascript arrays: Can you have both keys and values?

问题

我在JavaScript上对一个任意交易使用了ethers.providers.getLogs()函数。我得到了以下结果作为其中的一个参数。

这个数组中似乎有键值对(用方括号[]表示),但这看起来有些奇怪。这让我觉得这个数组像一个对象。但情况也不可能是这样,因为有些键是没有对应值的。

有人能解释这里发生了什么吗?

[
  '0xd2ab1089822171b728a5694b79acf292f6b59ff8c548d2a2e71f6c5ff7025f0f',
  '0xcd63cB2374e49f88083d79D5f7891be5734cdc68',
  '0x0000000000000000000000000000000000000000',
  BigNumber { _hex: '0x38', _isBigNumber: true },
  true,
  BigNumber { _hex: '0xc817c38c6ddc', _isBigNumber: true },
  BigNumber { _hex: '0x023c74', _isBigNumber: true },
  userOpHash: '0xd2ab1089822171b728a5694b79acf292f6b59ff8c548d2a2e71f6c5ff7025f0f',
  sender: '0xcd63cB2374e49f88083d79D5f7891be5734cdc68',
  paymaster: '0x0000000000000000000000000000000000000000',
  nonce: BigNumber { _hex: '0x38', _isBigNumber: true },
  success: true,
  actualGasCost: BigNumber { _hex: '0xc817c38c6ddc', _isBigNumber: true },
  actualGasUsed: BigNumber { _hex: '0x023c74', _isBigNumber: true }
]
英文:

I used an ethers.providers.getLogs() function on an arbitrary transaction in javascript. I got the following result as one of the parameters.

There seems to be key-pair values in this array (denoted by the brackets []), which doesn't make sense. This makes me think this as an object. But that can't be the case as well because there are singular keys without pairs.

Can someone explain what's going on here?

[
  '0xd2ab1089822171b728a5694b79acf292f6b59ff8c548d2a2e71f6c5ff7025f0f',
  '0xcd63cB2374e49f88083d79D5f7891be5734cdc68',
  '0x0000000000000000000000000000000000000000',
  BigNumber { _hex: '0x38', _isBigNumber: true },
  true,
  BigNumber { _hex: '0xc817c38c6ddc', _isBigNumber: true },
  BigNumber { _hex: '0x023c74', _isBigNumber: true },
  userOpHash: '0xd2ab1089822171b728a5694b79acf292f6b59ff8c548d2a2e71f6c5ff7025f0f',
  sender: '0xcd63cB2374e49f88083d79D5f7891be5734cdc68',
  paymaster: '0x0000000000000000000000000000000000000000',
  nonce: BigNumber { _hex: '0x38', _isBigNumber: true },
  success: true,
  actualGasCost: BigNumber { _hex: '0xc817c38c6ddc', _isBigNumber: true },
  actualGasUsed: BigNumber { _hex: '0x023c74', _isBigNumber: true }
]

答案1

得分: 1

这是一个表示数组的文本,其中还包含非整数键的属性,例如 Node.js 的 console.log(使用默认的 %O 格式)/util.inspect

@ThisGuy 在评论中指出的,

> 看起来数据出现了两次。一次是原始的、未标记的数据,然后第二组是使用键和值。

您所使用的库的创建者可能出于向后兼容性的考虑这样做,例如,如果最初函数返回的是数组,然后决定使用命名属性更好。

class BigNumber {
  constructor(hex) {
    this._hex = hex;
    this._isBigNumber = true;
  }
}

const exact = [
  '0xd2ab1089822171b728a5694b79acf292f6b59ff8c548d2a2e71f6c5ff7025f0f',
  '0xcd63cB2374e49f88083d79D5f7891be5734cdc68',
  '0x0000000000000000000000000000000000000000',
  new BigNumber('0x38'),
  true,
  new BigNumber('0xc817c38c6ddc'),
  new BigNumber('0x023c74'),
];

Object.assign(exact, {
  userOpHash: '0xd2ab1089822171b728a5694b79acf292f6b59ff8c548d2a2e71f6c5ff7025f0f',
  sender: '0xcd63cB2374e49f88083d79D5f7891be5734cdc68',
  paymaster: '0x0000000000000000000000000000000000000000',
  nonce: new BigNumber('0x38'),
  success: true,
  actualGasCost: new BigNumber('0xc817c38c6ddc'),
  actualGasUsed: new BigNumber('0x023c74'),
});

document.getElementById('formatted').textContent = util.inspect(exact);
<script src="https://unpkg.com/node-inspect-extracted/dist/inspect.js"></script>
<pre id="formatted"></pre>
英文:

This is a text representation of an array that also has non-integer-keyed properties, used by e.g. Node.js’s console.log (with the default %O format)/util.inspect.

As @ThisGuy noted in the comments,

> seems like the data is there twice. once as raw, unlabeled data. then a second set, using key and value

The creator of the library you’re using might be doing that for backwards compatibility, e.g. if the function originally returned an array but then it was decided that named properties were a better choice.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

class BigNumber {
  constructor(hex) {
    this._hex = hex;
    this._isBigNumber = true;
  }
}

const exact = [
  &#39;0xd2ab1089822171b728a5694b79acf292f6b59ff8c548d2a2e71f6c5ff7025f0f&#39;,
  &#39;0xcd63cB2374e49f88083d79D5f7891be5734cdc68&#39;,
  &#39;0x0000000000000000000000000000000000000000&#39;,
  new BigNumber(&#39;0x38&#39;),
  true,
  new BigNumber(&#39;0xc817c38c6ddc&#39;),
  new BigNumber(&#39;0x023c74&#39;),
];

Object.assign(exact, {
  userOpHash: &#39;0xd2ab1089822171b728a5694b79acf292f6b59ff8c548d2a2e71f6c5ff7025f0f&#39;,
  sender: &#39;0xcd63cB2374e49f88083d79D5f7891be5734cdc68&#39;,
  paymaster: &#39;0x0000000000000000000000000000000000000000&#39;,
  nonce: new BigNumber(&#39;0x38&#39;),
  success: true,
  actualGasCost: new BigNumber(&#39;0xc817c38c6ddc&#39;),
  actualGasUsed: new BigNumber(&#39;0x023c74&#39;),
});

document.getElementById(&#39;formatted&#39;).textContent = util.inspect(exact);

<!-- language: lang-html -->

&lt;script src=&quot;https://unpkg.com/node-inspect-extracted/dist/inspect.js&quot;&gt;&lt;/script&gt;
&lt;pre id=&quot;formatted&quot;&gt;&lt;/pre&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月2日 03:46:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定