Dev Helpers
← Back

Align rows by shared key

Align rows by shared key using JavaScript.

Open this example in the interactive library

Source code

const labels = new Map([
  [1, "Studio"],
  [2, "Print"],
]);
const totals = [
  { id: 2, n: 7 },
  { id: 1, n: 4 },
];
console.log(totals.map((r) => ({ ...r, label: labels.get(r.id) })));

Sample output

[ { id: 2, n: 7, label: 'Print' }, { id: 1, n: 4, label: 'Studio' } ]