访问嵌套的JSON对象

时间:2019-01-14 21:00:53

标签: javascript arrays object

这个问题已经被回答了无数次,但是出于某些该死的原因,我无法使其适用于我的情况。

我正在解析一个以太坊区块链,并且得到的结果存储在this.state.eventData状态下

数据如下:

[
    [{
            "logIndex": 0,
            "transactionIndex": 0,
            "transactionHash": "0xec912b5811f72b9e821fd62f7c79e45c09c641bb9bf4fff3be9e4997be27cd76",
            "blockHash": "0x84f988d6611ba75e8321e20abf23620d68efb0ff721b85447b8072cd5ff07525",
            "blockNumber": 16,
            "address": "0x985b025b6baa40c6d5c7247f4d608efdfc24b81b",
            "type": "mined",
            "event": "EventOne",
            "args": {
                "paramOne": "0x6a7a88d1f9f6250e1115d05a0489e584d0a0c7c0",
                "paramTwo": "90",
                "paramThree": "2",
                "paramFour": "479",
                "paramFive": "110123595505617976",
                "paramSix": "1",
                "paramSeven": true
            }
        },
        {
            "logIndex": 0,
            "transactionIndex": 0,
            "transactionHash": "0x776ecfd9a1efe0a0d399a4a3d56f2121d5305e4d3219c13ca4e960bcdcce460c",
            "blockHash": "0x109907689e47d96a61bffc0ec4eac5cf4295361d57c9a25fe53aa68e1412eadc",
            "blockNumber": 18,
            "address": "0x985b025b6baa40c6d5c7247f4d608efdfc24b81b",
            "type": "mined",
            "event": "EventOne",
            "args": {
                "paramOne": "0x6a7a88d1f9f6250e1115d05a0489e584d0a0c7c0",
                "paramTwo": "90",
                "paramThree": "17",
                "paramFour": "480",
                "paramFive": "110123595505617976",
                "paramSix": "2",
                "paramSeven": true
            }
        }
    ]
]

我通过console.log(eventData)获得了这些数据,然后从Chrome控制台复制了对象。enter code here

我想简单地得到每个对象的paramOne。当我尝试简单地console.log(this.state.eventData[[0].args])时,出现了未定义的错误。

感谢您在这场斗争中的帮助。

2 个答案:

答案 0 :(得分:0)

要检索所有这些文件,可以在map数组上使用this.state.eventData[0]

this.state.eventData[0].map(event => event.args.paramOne)
// ["0x6a7a88d1f9f6250e1115d05a0489e584d0a0c7c0", "0x6a7a88d1f9f6250e1115d05a0489e584d0a0c7c0"]

答案 1 :(得分:-1)

const data = [
    [{
            "logIndex": 0,
            "transactionIndex": 0,
            "transactionHash": "0xec912b5811f72b9e821fd62f7c79e45c09c641bb9bf4fff3be9e4997be27cd76",
            "blockHash": "0x84f988d6611ba75e8321e20abf23620d68efb0ff721b85447b8072cd5ff07525",
            "blockNumber": 16,
            "address": "0x985b025b6baa40c6d5c7247f4d608efdfc24b81b",
            "type": "mined",
            "event": "EventOne",
            "args": {
                "paramOne": "0x6a7a88d1f9f6250e1115d05a0489e584d0a0c7c0",
                "paramTwo": "90",
                "paramThree": "2",
                "paramFour": "479",
                "paramFive": "110123595505617976",
                "paramSix": "1",
                "paramSeven": true
            }
        },
        {
            "logIndex": 0,
            "transactionIndex": 0,
            "transactionHash": "0x776ecfd9a1efe0a0d399a4a3d56f2121d5305e4d3219c13ca4e960bcdcce460c",
            "blockHash": "0x109907689e47d96a61bffc0ec4eac5cf4295361d57c9a25fe53aa68e1412eadc",
            "blockNumber": 18,
            "address": "0x985b025b6baa40c6d5c7247f4d608efdfc24b81b",
            "type": "mined",
            "event": "EventOne",
            "args": {
                "paramOne": "0x6a7a88d1f9f6250e1115d05a0489e584d0a0c7c0",
                "paramTwo": "90",
                "paramThree": "17",
                "paramFour": "480",
                "paramFive": "110123595505617976",
                "paramSix": "2",
                "paramSeven": true
            }
        }
    ]
]
data[0].forEach((item) => {
  console.log(item.args.paramOne)
})