-1

This has been answered numerous times, but for some damn reason I can't make it work in my case.

I'm parsing an Ethereum blockchain and getting a result that I am storing in a state as this.state.eventData

The data looks like this:

[
    [{
            "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
            }
        }
    ]
]

I got this data by console.log(eventData) and then copying the object from the Chrome console.enter code here

I want to simply get, say, paramOne of each object. When I'm trying to simply console.log(this.state.eventData[[0].args]) I'm getting an undefined error.

I'd appreciate your help in this struggle.

Ruham
  • 749
  • 10
  • 32
  • well... you've got an array in an array. So at the very least you'll need two separate indexes before you try to access the args property. `foo[x][y].args` – Kevin B Jan 14 '19 at 21:04
  • will it be then this.state.eventData[0][0].args? I've tried that, and it still gives me an error. – Ruham Jan 14 '19 at 21:05
  • Yes, but i can't guarentee that you're accessing `this.state.eventData` after it has been populated. My guess is you're accessing it too soon. – Kevin B Jan 14 '19 at 21:06
  • I am console.logging this.state.eventData, and it shows the populated object in the console, but when I change the value of console.log() by adding those params, it gives me the undefined error. – Ruham Jan 14 '19 at 21:07
  • Yeah, i think you're being fooled by the console into thinking it's populated, when it actually isn't. – Kevin B Jan 14 '19 at 21:08
  • But how come it's not if I copied the object from the console and got the data to past here? Sorry for the dumb questions, I just not completely understand. – Ruham Jan 14 '19 at 21:10
  • 1
    Because the console has this feature where if you console.log an object or an array, it doesn't inspect the object or array until you click in the console to expand it. At the point in time you expand it, it looks at the contents of the array or object and displays it to you. If you instead logged, for example, the length of the array, you would find that it is in fact 0, not 1, at that point in time. – Kevin B Jan 14 '19 at 21:11

1 Answers1

0

To retrieve all of them you can use map on the this.state.eventData[0] array:

this.state.eventData[0].map(event => event.args.paramOne)
// ["0x6a7a88d1f9f6250e1115d05a0489e584d0a0c7c0", "0x6a7a88d1f9f6250e1115d05a0489e584d0a0c7c0"]
0xc14m1z
  • 3,675
  • 1
  • 14
  • 23