# Filtering the highest value key from an object

**URL:** https://discourse.nodered.org/t/filtering-the-highest-value-key-from-an-object/56257
**Category:** General
**Created:** [7 January 2022 14:14 UTC](https://discourse.nodered.org/t/filtering-the-highest-value-key-from-an-object/56257 "2022-01-07T14:14:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bradds](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bradds/32/3238_2.png) [@bradds](https://discourse.nodered.org/u/bradds)
#### Post date: [7 January 2022 14:14 UTC](https://discourse.nodered.org/t/filtering-the-highest-value-key-from-an-object/56257/1 "2022-01-07T14:14:10Z")

</div>

Hi.

I am trying to filter out the key with the highest number from this object.  
I have tried to do it with a split node, but it does not give the key, just the number.  
Been searching the forum but nothing I found works.  
Does anyone know how to do this with a quick function node so that the output will show the "key AND number" of the highest value only?

In this case the output of msg.payload would be (neutral : 0.976....)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/c/3c0fa50b2ea68e9d6a99236150918612d536c298.png)

Thanks in advance.

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [7 January 2022 14:35 UTC](https://discourse.nodered.org/t/filtering-the-highest-value-key-from-an-object/56257/2 "2022-01-07T14:35:50Z")

</div>

Set up a `change` node to use this JSONata expression (the `J:` option):

```
payload.expressions.$spread()^(>*)[0]

```

I know, so readable, right? wth?!?

In human terms, it "spreads" the object fields into an array of single-field objects, then sorts them descending by value, and returns only the first one. If you have not looked at JSONata, it uses a very powerful lambda expression syntax to restructure JSON objects/arrays (similar to using XPath for XML data). It takes an hour or so to wrap your head around, but well worth knowing how to use in node-red, imo.

---

<div class="post-metadata">

### Author: ![bradds](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bradds/32/3238_2.png) [@bradds](https://discourse.nodered.org/u/bradds)
#### Post date: [7 January 2022 14:47 UTC](https://discourse.nodered.org/t/filtering-the-highest-value-key-from-an-object/56257/3 "2022-01-07T14:47:32Z")

</div>

> [@shrickus](#):
>
> `payload.expressions.$spread()^(>*)[0]`

Thanks for the quick response and detailed expanation @shrickus  
That was just what I wanted, easy when you know how...

---

<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: [21 January 2022 14:48 UTC](https://discourse.nodered.org/t/filtering-the-highest-value-key-from-an-object/56257/4 "2022-01-21T14:48:26Z")

</div>

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