i have a simple array of objects like this
{
"a": [1,2],
"b": [3,4]
}
and I want to put it in this form
[
{ "a": 1, "b": 3},
{ "a": 2, "b": 4}
]
I want to find the right approach here.
Seems like this is a transpose operation where a table is given as an object where the keys are the column names and the values is the column data. The output is supposed to be an array of table rows where each row is an object with the column names are its keys.
(
/* define function that iterates over all columns, gets the i-th array index and merges the results */
$getRow := function($x, $i){
$merge($each($x, function($v, $k) {
{$k: $v[$i]}
}))
};
/* this is the first key that is found, "a" in the example */
$firstKey := $.$keys()[0];
/* this is the first column, [1,2] in the example */
$firstArray := $lookup($, $firstKey);
/* loop over the first array (index is $i) and call $getRow($, $i) */
$map($firstArray, function($v, $i){
$getRow($,$i)
} );
)