Well, I don't know of any "native" function, but I generally like to use a JSONata expression to morph one structure into another -- I plugged your data into the jsonata exerciser site and developed this expression:
payload~>$map(function($arr, $ind) {
(
$tmp := $map($arr, function($v, $i) {
{"index": $i, "value": $number($v)}
});
{
$string($ind): [
[$tmp[value > 0].index],
[$tmp[value > 0].value]
]
}
)
})~>$merge()
The output seems to match your second (optimized) output style... if you put this into a change
node,
just make sure to select the J:
pulldown, click the three dots ...
to open the edit panel, and paste the expression. There is even an expression "tester" tab where you can put your actual payload data and see how the output changes as you modify the expression:
Or you can simply import this node...
[{"id":"e496cac4.80f788","type":"change","z":"d59690cd.1ee8c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload~>$map(function($arr, $ind) {\t\t(\t \t$tmp := $map($arr, function($v, $i) {\t \t{\"index\": $i, \"value\": $number($v)}\t });\t {\t \t$string($ind): [\t\t [$tmp[value > 0].index],\t \t [$tmp[value > 0].value]\t ]\t }\t )\t})~>$merge()\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1560,"y":320,"wires":[[]]}]