Select last item array where key has a value over a certain number

Example flow:

[
  {
    "gross": 7900,
    "sale": {
      "settled": true
    }
  },
  {
    "gross": 209400,
    "sale": {
      "settled": true
    }
  },
  {
    "gross": 35000,
    "sale": {
      "settled": true
    }
  },
  {
    "gross": 2094000,
    "sale": {
      "settled": false
    }
  },
  {
    "gross": 35000,
    "sale": {
      "settled": true
    }
  }
]

I want a function node that looks for the value of settled in the latest array item where gross is equal to or larger than the number 35100. And allows the message to pass through if settled equals false.

So in this example, the message would pass because the last item where gross is larger than 35100 (array item 3) has a settled value of false.

In this example however, the message would not pass because the same item has a settle value of true, even though the last item in the array has false.

[
  {
    "gross": 7900,
    "sale": {
      "settled": true
    }
  },
  {
    "gross": 209400,
    "sale": {
      "settled": true
    }
  },
  {
    "gross": 35000,
    "sale": {
      "settled": true
    }
  },
  {
    "gross": 2094000,
    "sale": {
      "settled": true
    }
  },
  {
    "gross": 35000,
    "sale": {
      "settled": false
    }
  }
]

Have a look at my solution using find() in your other post to find the first matching element in an array. Make sure you understand how that works (ask there for help if you can't understand it after looking at the docs for Array.find()) and then have a go at solving this problem using findLast() which is exactly the same as find() except that it finds the last matching instead of the first.

Tried this code to select the last item in the array where gross is equal to or larger than 351000. And then setting the payload to that item, (for further processing using a switch node):

const LastMainInvoice = msg.payload.findLast((ArrayEntry) => ArrayEntry.gross >= 351000);
let answer = LastMainInvoice

return answer

But get the following error:

TypeError: msg.payload.findLast is not a function

If msg.payload.findLast is not a function, then msg.payload is not an array. Therefore either the data is not as you showed, or it is not in msg.payload.

Also you must always return a message, so at the end you probably want

msg.payload = answer
return msg
``

Or you are not using Node.js v18 :grin: as I believe that was the version that introduced it.

Ah, good point.
@network_potato, if you are not using nodjs 18 (run node -v to find out) then you can use
msg.payload.reverse().find((ArrayEntry) => ArrayEntry.gross >= 351000);
which reverses the array and then finds the first one.

Ah, yes my version is v16.16.0.

Anyway, I managed to do it by using a split node, removing the unwanted items from the array with a switch node, and then joining it back together. Then selecting the last item in the array using a JSONata expression: $$.payload[-1].sale.settled.

Did you try my alternative solution?

You should be able to it without the split, using change node and JSONata expression

$$.payload[$.gross >= 35100][-1][$.sale.settled = false]

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.