JSONATA subtraction in arrays

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

[
    [
        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

*[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!

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

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

Node REPL demo
image

1 Like

The Jsonata would be

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

output

[
    120000,
    180000,
    -60000,
    120000
]
2 Likes

Great, thank you!

Thank you!

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