Prism · Structure

$keyBy

Turns a list of records into an object keyed by a value computed for each. Where two records give the same key, the later one stays.

The source
{
  products: [
    { sku: 'A1', name: 'Apple', price: 1.5 },
    { sku: 'B1', name: 'Banana', price: 0.5 },
    { sku: 'C1', name: 'Cherry', price: 3 },
  ],
}
The config
{
  $keyBy: {
    over: { $ref: '$.products' },
    as: 'p',
    key: { $get: { from: { $var: 'p' }, path: ['sku'] } },
  },
}
What comes out
{
  A1: { sku: 'A1', name: 'Apple', price: 1.5 },
  B1: { sku: 'B1', name: 'Banana', price: 0.5 },
  C1: { sku: 'C1', name: 'Cherry', price: 3 },
}
Output matches the expected value

Runs in this page on the installed package. The input and the expected output are defined by the example.