Registry / serialization / index-array-by

index-array-by

JSON →
library1.4.2jsnpmunverified

index-array-by is a lightweight utility function, currently at version 1.4.2, designed for efficiently indexing JavaScript arrays of objects based on various criteria. It enables developers to transform a flat list into a nested object or array structure. Key differentiators include its flexibility in defining index keys, supporting string property names, an array of property names for hierarchical indexing, or custom accessor functions for complex key generation. The library also provides robust handling for duplicate keys, allowing the user to specify whether each index key should map to a single item (potentially overwriting duplicates if not unique) or an array of all matching items (the default behavior). Furthermore, it supports custom transformation functions for reducing multiple items into a single value, and an optional parameter to flatten the output into an array of { keys, vals } objects. It is actively maintained, shipping with TypeScript types to enhance developer experience and provide strong type checking in modern JavaScript environments.

npm install index-array-by
INSTALL
IMPORT
SIG · INDEX-ARRAY-BY
I
index-array-by
serializationjavascriptv1.4.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

indexBy
import indexBy from 'index-array-by';
import { indexBy } from 'index-array-by';
indexBy is a default export. TypeScript types are included, providing type-safety for arguments and return values.
indexBy (CommonJS)
const indexBy = require('index-array-by');
For CommonJS environments, 'require' can be used directly. The package is compatible with both CJS and ESM.

Demonstrates indexing an array of objects by a string property, resulting in an object where each key points to a single item, preventing array wrapping.

import indexBy from 'index-array-by'; const people = [ { name: 'Mary', surname: 'Jane', age: 28 }, { name: 'John', surname: 'Smith', age: 24 }, { name: 'John', surname: 'Doe', age: 32 } ]; // Index the 'people' array by 'surname', ensuring each key maps to a single item const indexedBySurname = indexBy(people, 'surname', false); console.log(indexedBySurname); /* // Expected Output: { Doe: { name: 'John', surname: 'Doe', age: 32 }, Jane: { name: 'Mary', surname: 'Jane', age: 28 }, Smith: { name: 'John', surname: 'Smith', age: 24 } } */
Debug
Known issues
gotchaThe third parameter, 'multiItem', defaults to 'true'. If you expect keys to map to single items, you must explicitly set 'multiItem' to 'false' to avoid values being wrapped in arrays even for unique keys.
fix
Call indexBy with `indexBy(list, keyAccessor, false)` to ensure single-item values.
affects: >=1.0.0
gotchaWhen 'multiItem' is set to 'false', if multiple items share the same key, only the last item processed for that key will be retained, effectively overwriting previous entries.
fix
Ensure your keyAccessor generates unique keys or set 'multiItem' to 'true' (default) to collect all items into an array, or provide a custom reducer function for 'multiItem' to handle conflicts.
affects: >=1.0.0
gotchaUsing function-based key accessors that return non-string/non-symbol values can lead to unexpected object key coercions (e.g., numbers becoming strings, objects becoming '[object Object]').
fix
Ensure custom key accessor functions explicitly return string or symbol values for predictable object indexing.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: indexBy is not a function
Attempting to import the default export as a named export or using CommonJS require incorrectly in an ESM context.
fix
Use `import indexBy from 'index-array-by';` for ESM or ensure proper CommonJS require if not in an ESM environment (e.g., `const indexBy = require('index-array-by');`).
Output object has array values like { key: [item] } even when expecting single items.
The 'multiItem' parameter defaults to 'true', always returning an array of items for each key, even if only one item matches.
fix
Set the third argument 'multiItem' to 'false': `indexBy(yourArray, 'yourKey', false)`.
Items are unexpectedly missing from the indexed object, or only the last item with a duplicate key is present.
When 'multiItem' is set to 'false' (or a single-item reducer is used) and multiple input items resolve to the same key, subsequent items overwrite earlier ones.
fix
If all items sharing a key should be kept, set 'multiItem' to 'true'. If a single item is desired, ensure keys are unique or provide a custom reducer function to 'multiItem' to define how multiple items are consolidated.
Upgrade
Version history
1.4.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources
index-array-by — npm install index-array-by · libregistry