-1

I'm reading the process being built on

https://developers.cardano.org/docs/integrate-cardano/listening-for-payments-cli/#process-utxo-table

and I'm stuck on the results of this part:

// Calculate total lovelace of the UTXO(s) inside the wallet address
const utxoTableRows = rawUtxoTable.data.trim().split('\n');
let totalLovelaceRecv = 0;
let isPaymentComplete = false;

for (let x = 2; x < utxoTableRows.length; x++) {
    const cells = utxoTableRows[x].split(" ").filter(i => i);
    totalLovelaceRecv += parseInt(cells[2]);
}

I understand that up to the .split(" ") part, the table has been split into at least 2 items within the array of utxoTableRows since the first row the headers that we aren't interested in and the second row is the first utxo being processed. The .split(" ") then splits the row where there is a space but I don't know what the filter part does. The query utxo table looks as follows:

TxHash TxIx Amount
dfb99f8f103e56a856e04e087255dbaf402f3801acb71a6baf423a1054d3ccd5 0 1749651926

As you can see there are two rows. I'm completely lost at the filter component. I get more lost in the following command parseInt(cells[2]). I know somehow it takes the amount as a string and converts to integer. The [2] reference throws me off because wouldn't it have to be [3] since the amount is the 3rd item that gets split in that row? The only thing I can think of is that the .filter(i => i) throws out the TxHash, then saves the TxIx (in this case 0) in cell[1] and the amount (in this case 1749651926) in cell[2], both as strings.

Thank you for the help.

pilchard
  • 12,414
  • 5
  • 11
  • 23
  • This is why I prefer `.filter(Boolean)` or being very explicit (`.filter(i => i !== '')`). – Felix Kling Oct 20 '21 at 23:34
  • Does this answer your question? [How to remove empty array values ("") from an array?](https://stackoverflow.com/questions/55685037/how-to-remove-empty-array-values-from-an-array) – pilchard Oct 20 '21 at 23:39

1 Answers1

1

filter will filter out all items in the array where the callback returns false after coercion.

In this case, the filter removes all blank strings in the array. If the item is an empty string, the callback returns false, since empty strings, when coerced to a boolean, are false (see: Falsy values).

Spectric
  • 30,714
  • 6
  • 20
  • 43