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

**URL:** https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862
**Category:** General
**Created:** [23 March 2023 11:18 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862 "2023-03-23T11:18:36Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [23 March 2023 11:18 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862/1 "2023-03-23T11:18:37Z")

</div>

Example flow:

```auto
[
  {
    "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`.

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

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [23 March 2023 11:38 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862/2 "2023-03-23T11:38:39Z")

</div>

Have a look at my solution using `find()` in [your other post](https://discourse.nodered.org/t/set-payload-to-first-occurence-of-certain-key-value-in-array/76857) 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.

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [23 March 2023 13:30 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862/3 "2023-03-23T13:30:05Z")

</div>

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):

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

return answer

```

But get the following error:

```auto
TypeError: msg.payload.findLast is not a function

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [23 March 2023 13:46 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862/4 "2023-03-23T13:46:45Z")

</div>

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

```auto
msg.payload = answer
return msg
``
```

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [23 March 2023 14:21 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862/5 "2023-03-23T14:21:03Z")

</div>

> [@Colin](#):
>
> If `msg.payload.findLast` is not a function, then `msg.payload` is not an array.

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [23 March 2023 14:36 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862/6 "2023-03-23T14:36:56Z")

</div>

> [@TotallyInformation](#):
>
> not using Node.js v18 😁 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.

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [23 March 2023 21:01 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862/7 "2023-03-23T21:01:26Z")

</div>

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`.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [23 March 2023 21:59 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862/8 "2023-03-23T21:59:56Z")

</div>

Did you try my alternative solution?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [23 March 2023 22:10 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862/9 "2023-03-23T22:10:15Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [22 May 2023 22:10 UTC](https://discourse.nodered.org/t/select-last-item-array-where-key-has-a-value-over-a-certain-number/76862/10 "2023-05-22T22:10:49Z")

</div>

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