Search code examples
javascriptjqueryjsonethereum

How can I select specific items from a json array?


So my

getTransactionFromAddress(Address).then((res) => {
  console.log(res);
})

spits out a JSON file that looks like this:

    {
  status: '1',
  message: 'OK',
  result: [
    {
      blockNumber: '11381906',
      timeStamp: '1607028366',
      hash: '0x8d549d97116373ef3a0fc5b1459232db75200c9b8a8a90c04bccfe8c5e91a7d9',
      nonce: '422',
      blockHash: '0x2d63d2c6b63bc6763ed7aa7df6aa7311be64a361c01d57c443496ccd4018bbdc',
      transactionIndex: '189',
      from: '0x692190b4a5d3524b6fed0465e7400c07d09db954',
      to: '0x88a7ef2f047f8b07c6c917a6fd1a13438e9d8424',
      value: '10000000000000000',
      gas: '42000',
      gasPrice: '38000000000',
      isError: '0',
      txreceipt_status: '1',
      input: '0x',
      contractAddress: '',
      cumulativeGasUsed: '12387171',
      gasUsed: '21000',
      confirmations: '1068839'
    }

I want to select specific items and put display the values on a dashboard

I am trying to do it with :

console.table([{
        "Time": res.result.timeStamp,
        "From": res.result.from,
        "To": res.result.to,
        "Gas": res.result.gasPrice,
        "Value:" : res.result.value
      }]);

But I am not getting anything. Can someone please show me how to do this?


Solution

  • Try this:

    console.table([{
            "Time": res.result[0].timeStamp,
            "From": res.result[0].from,
            "To": res.result[0].to,
            "Gas": res.result[0].gasPrice,
            "Value:" : res.result[0].value
          }]);
    

    Considering that your result is an array of data and that it has only one object in it, you should use brackets annotation in order to pull the data from the first index of that array