# JSONATA subtraction in arrays

**URL:** <https://discourse.nodered.org/t/jsonata-subtraction-in-arrays/74593>\
**Category:** General\
**Created:** [2 February 2023 09:54 UTC](https://discourse.nodered.org/t/jsonata-subtraction-in-arrays/74593 "2023-02-02T09:54:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobiasselin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobiasselin/32/4492_2.png) [@tobiasselin](https://discourse.nodered.org/u/tobiasselin)\
**Post date:** [2 February 2023 09:54 UTC](https://discourse.nodered.org/t/jsonata-subtraction-in-arrays/74593/1 "2023-02-02T09:54:36Z")

</div>

Hello all. I have an array of arrays like the one below and I would like to subtract the second object in the inner arrays from the first

```auto
[
    [
        1675324800000,
        1675324680000
    ],
    [
        1675324980000,
        1675324800000
    ],
    [
        1675324800000,
        1675324860000
    ],
    [
        1675324980000,
        1675324860000
    ]

] 

```

I can't get the jsonata to work here. I've tried wildcards in different ways and have found that

```auto
*[1]-*[0]

```

gives me the result for the first inner array. However, I've not been able to have it go through the whole array of arrays.

Would appreciate some help!

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 February 2023 10:08 UTC](https://discourse.nodered.org/t/jsonata-subtraction-in-arrays/74593/2 "2023-02-02T10:08:47Z")

</div>

Hi, I am certain it can be done but I rarely use JSONata for anything more than simple maths and string concatination

In plain old JS, it is simple (and faster)...

```auto
msg.payload = msg.payload.map(e => e[0] - e[1])
return msg

```

Node REPL demo  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/61c13a23c47d6c05fdf65308ee47f1932e3f5826.png)

---

<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:** [2 February 2023 10:17 UTC](https://discourse.nodered.org/t/jsonata-subtraction-in-arrays/74593/3 "2023-02-02T10:17:35Z")

</div>

The Jsonata would be

```auto
$$.payload.($[0]-$[1])

```

output

```auto
[
    120000,
    180000,
    -60000,
    120000
]

```

---

<div class="post-metadata">

**Author:** ![tobiasselin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobiasselin/32/4492_2.png) [@tobiasselin](https://discourse.nodered.org/u/tobiasselin)\
**Post date:** [2 February 2023 11:16 UTC](https://discourse.nodered.org/t/jsonata-subtraction-in-arrays/74593/4 "2023-02-02T11:16:02Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> msg.payload = msg.payload.map(e => e[0] - e[1])
> return msg
> 
> ```

Great, thank you!

---

<div class="post-metadata">

**Author:** ![tobiasselin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobiasselin/32/4492_2.png) [@tobiasselin](https://discourse.nodered.org/u/tobiasselin)\
**Post date:** [2 February 2023 11:16 UTC](https://discourse.nodered.org/t/jsonata-subtraction-in-arrays/74593/5 "2023-02-02T11:16:19Z")

</div>

Thank you!

---

<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:** [16 February 2023 11:16 UTC](https://discourse.nodered.org/t/jsonata-subtraction-in-arrays/74593/6 "2023-02-16T11:16:26Z")

</div>

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