1

我有一个像这样的简单对象数组

{
   "a": [1,2],
   "b": [3,4]
}

我想把它放在这种形式

[
   { "a": 1, "b": 3},
   { "a": 2, "b": 4}
]

我想在这里找到正确的方法。

4

1 回答 1

1

似乎这是一个转置操作,其中将表作为对象给出,其中键是列名,值是列数据。输出应该是一个表行数组,其中每一行都是一个对象,列名是它的键。

(
    /* 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)
    } );
)
于 2022-02-05T11:44:37.467 回答