jsonata-js/jsonata

Question on $zip argument

robinmackenzie opened this issue · 0 comments

With:

$zip([1, 2, 3], [4, 5, 6], [7, 8, 9])

It produces the expected result:

[
  [1, 4, 7],
  [2, 5, 8],
  [3, 6, 9]
]

How should it work here for example:

(
    $arr := [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
    $zip($arr);
)

Which produces:

[
  [
    [1, 2, 3]
  ],
  [
    [4, 5, 6]
  ],
  [
    [7, 8, 9]
  ]
]

?

My use case is that the array I want to pass to $zip will have an unknown number of inner arrays.

I'm using the JSONata Exerciser v2.0.2.

Noting that I did look here:

jsonata/src/functions.js

Lines 1570 to 1593 in b520b88

/**
* Convolves (zips) each value from a set of arrays
* @param {Array} [args] - arrays to zip
* @returns {Array} Zipped array
*/
function zip() {
// this can take a variable number of arguments
var result = [];
var args = Array.prototype.slice.call(arguments);
// length of the shortest array
var length = Math.min.apply(Math, args.map(function (arg) {
if (Array.isArray(arg)) {
return arg.length;
}
return 0;
}));
for (var i = 0; i < length; i++) {
var tuple = args.map((arg) => {
return arg[i];
});
result.push(tuple);
}
return result;
}

And realised if I was using javascript then I could use the spread syntax:

const arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const result = zip(...arr);

But researching the documentation/ stack overflow etc - I didn't see a way to do that with jsonata syntax.

Thanks in advance.