1

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.

1 Answer 1

1

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)
    } );
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.