Example 1 — Official Sample
Turn two records into an object keyed by their id, as in the Ramda docs.
const list = [{ id: 'xyz', title: 'A' }, { id: 'abc', title: 'B' }];
const byId = R.indexBy(R.prop('id'), list);
console.log(JSON.stringify(byId)); How It Works
- Key function.
R.prop('id')returns'xyz'for the first item and'abc'for the second. - Store. Each whole item is saved under its key.
- Order. Keys appear in list order (
xyzfirst). The docs comment listsabcfirst, but the contents are identical. - No mutation.
listis unchanged;byIdis a new object.
