Scriptdrill

Level 4: Data shaping Exercise 30 of 45 Pro

Pivot rows into a lookup

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Turn flat rows into a two-level lookup: `result[row[rowKey]][row[colKey]]` is the sum of `row[valueKey]` over every row with that pair. Pairs that never appear are absent. A sum of 0 is still kept.

What this exercise teaches

Two levels of "create when missing" build the nested object in one pass, and adding instead of assigning sums duplicate pairs. Starting from 0 with `?? 0` keeps sums that cancel out to 0.

A hint if you get stuck

For each row, create `out[r]` as `{}` when missing, then add the value to `out[r][c]`, starting from 0.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Data shaping