0

Looking for some help with this Javascript code which is part of processing a JSON API call ...

Firstly some background - I'm attempting to process a nested array of events relating to a supplied address. In the array there are multiple events, some with the same timestamp. I am trying to iterate through the array and create a new object containing the event. Where a duplicate timetamp is found, the duplicate event should be added to the existing new object I created.

Problem - in the part regarding adding the event to the existing array of events in my new object - the push appears to work and yet the console.log entry shows some confusing information. It suggests that there's 2 items in the array, but when the array expanded - there isn't. When the front end code processes my new obbjects it also only find 1 item in the array.

Just registered so can't embed images. Link shows the console screenshot and code is here:

function processTransactions(address, json){

  // Get the tranasction array at data.items 
  var transactions = json.data.items;

  var txList = new Array();

  // For each item iterate through the log_events array and keep only those we match with (To or From)
  for (let i=0; i<transactions.length; i++){

    // tx is the current transaction being processed
    var tx = transactions[i];

    // Loop through the array of event logs in this transaction
    // looking for ones matching our address in the event logs
    for( let j=0; j<tx.log_events.length; j++){
      // j is an event in the array of events

      // Add this event to our processed List of events
      var e = tx.log_events[j];

      var a = e.raw_log_topics;
      var expr = new RegExp(address.substring(2));

      // Check if the address is contained in contents of raw_log_events
      if (a.find(value => expr.test(value))){

        if (txList.some(ev => ev.block_signed_at === e.block_signed_at)){
          // Matching object found in txList
          console.log ("Match found using timestamp");
          //console.log(txList[index].events);
          var o = txList.find( ev => ev.block_signed_at === e.block_signed_at );
          o.events.push(e);

          console.log(o);
          console.log("ending match found")

        } else {
          // No match found, create object and add to txList
          var o = {
            block_signed_at: e.block_signed_at,
            // Add unixtime timestamp to the object for display and sorting in frontend 
            block_signed_at_timestamp: new Date(e.block_signed_at).valueOf() / 1000,
            events: []
          }
          o.events.push(e);
          txList.push(o);
        };
        
      } // End of if
    } // End of for loop
  } // End of the for loop
  return {transactions: txList};
}

Screenshot of console.log

Steve
  • 1
  • 1
    https://stackoverflow.com/questions/23392111/console-log-async-or-sync – Teemu Nov 25 '21 at 10:46
  • Its Google chrome `console.log` inconsistency with objects and arrays. For ref visit this URL https://stackoverflow.com/questions/24175017/google-chrome-console-log-inconsistency-with-objects-and-arrays – Sateesh Nov 25 '21 at 10:55
  • Thanks both - that's certainly helped to clear it up... fixed using console.log(JSON.parse(JSON.stringify(o))); – Steve Nov 25 '21 at 14:17

0 Answers0