{"id":29751746,"url":"https://github.com/vasturiano/index-array-by","last_synced_at":"2025-07-26T14:42:02.598Z","repository":{"id":29845567,"uuid":"122934957","full_name":"vasturiano/index-array-by","owner":"vasturiano","description":"A utility function to index arrays by any criteria","archived":false,"fork":false,"pushed_at":"2025-03-21T22:38:45.000Z","size":183,"stargazers_count":10,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-21T10:43:03.518Z","etag":null,"topics":["array","data-transformation","index"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vasturiano.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-02-26T08:04:50.000Z","updated_at":"2025-04-09T04:01:34.000Z","dependencies_parsed_at":"2024-01-14T00:21:33.521Z","dependency_job_id":"b7a66a74-3510-46dd-a583-a874ef74abab","html_url":"https://github.com/vasturiano/index-array-by","commit_stats":{"total_commits":43,"total_committers":2,"mean_commits":21.5,"dds":0.2325581395348837,"last_synced_commit":"42af35b19a3ee6fd146ab16792d32ec876567856"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/vasturiano/index-array-by","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vasturiano%2Findex-array-by","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vasturiano%2Findex-array-by/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vasturiano%2Findex-array-by/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vasturiano%2Findex-array-by/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vasturiano","download_url":"https://codeload.github.com/vasturiano/index-array-by/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vasturiano%2Findex-array-by/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266489785,"owners_count":23937370,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","data-transformation","index"],"created_at":"2025-07-26T14:42:01.160Z","updated_at":"2025-07-26T14:42:02.590Z","avatar_url":"https://github.com/vasturiano.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"index-array-by\n==============\n\n[![NPM package][npm-img]][npm-url]\n[![Build Size][build-size-img]][build-size-url]\n[![NPM Downloads][npm-downloads-img]][npm-downloads-url]\n\nA utility function to index arrays by any criteria.\n\n`indexBy(list, keyAccessors, multiItem = true)`\n\n## Quick start\n\n```js\nimport indexBy from 'index-array-by';\n```\nor using a *script* tag\n```html\n\u003cscript src=\"//cdn.jsdelivr.net/npm/index-array-by\"\u003e\u003c/script\u003e\n```\n\n## Usage example\n\nGiven an array\n```js\nconst people = [\n    { name: 'Mary', surname: 'Jane', age: 28 },\n    { name: 'John', surname: 'Smith', age: 24 },\n    { name: 'John', surname: 'Doe', age: 32 }\n];\n```\n\nUse `indexBy` to index it by a given attribute (string type `keyAccessor`) or any other custom criteria (function type `keyAccessor`). You can also pass an array of `keyAccessors` to retrieve a nested object recursively indexed by the multiple keys.\n\nUse the third parameter (`multiItem`) to indicate whether each key should point to a single item (unadvised if the keys are not unique) or an array of multiple items (default behavior). \n\n```js\nindexBy(people, 'surname', false);\n\n// Result: \n{\n Doe: { name: 'John', age: 32 },\n Jane: { name: 'Mary', age: 28 },\n Smith: { name: 'John', age: 24 }\n}\n```\n\n```js\nindexBy(people, 'name', true);\n\n// Result: \n{\n  Mary: [ { surname: 'Jane', age: 28 } ],\n  John: [\n    { surname: 'Smith', age: 24 },\n    { surname: 'Doe', age: 32 }\n  ]\n}\n```\n\n```js\nindexBy(people, ({ name, surname }) =\u003e `${surname}, ${name}`, false);\n\n// Result: \n{\n 'Jane, Mary': { name: 'Mary', surname: 'Jane', age: 28 },\n 'Smith, John': { name: 'John', surname: 'Smith', age: 24 },\n 'Doe, John': { name: 'John', surname: 'Doe', age: 32 }\n}\n```\n\n```js\nindexBy(people, ['name', 'surname'], false));\n\n// Result: \n{\n Mary: { Jane: { age: 28 }},\n John: { Smith: { age: 24 }, Doe: { age: 32 }}\n}\n```\n\n```js\nindexBy(people, ({ age }) =\u003e `${Math.floor(age / 10) * 10}s`, true);\n\n// Result: \n{\n  '20s': [\n    { name: 'Mary', surname: 'Jane', age: 28 },\n    { name: 'John', surname: 'Smith', age: 24 },\n  ],\n  '30s': [{ name: 'John', surname: 'Doe', age: 32 }]\n}\n```\n\n\nThe `multiItem` parameter also accepts a transformation function with the method to reduce multiple items into a single one. In this case, it's keeping only the max age.\n\n```js\nindexBy(people, 'name', items =\u003e Math.max(...items.map(item =\u003e item.age)));\n\n// Result:\n\n{\n  John: 32,\n  Mary: 28\n}\n```\n\n\nA fourth optional parameter (`flattenKeys`) (default: `false`) allows you to receive a flat array structure instead of the default nested format, with each item formatted as `{ keys: [\u003cordered unique keys for the item\u003e], vals: \u003csingle or multiple item\u003e }`.\n\n```js\nindexBy(people, ['name', 'surname'], true, true));\n\n// Result: \n[\n  { keys: ['Mary', 'Jane'], vals: [{ age: 28 }] },\n  { keys: ['John', 'Smith'], vals: [{ age: 24 }] },\n  { keys: ['John', 'Doe'], vals: [{ age: 32 }] }\n]\n```\n\n\n[npm-img]: https://img.shields.io/npm/v/index-array-by\n[npm-url]: https://npmjs.org/package/index-array-by\n[build-size-img]: https://img.shields.io/bundlephobia/minzip/index-array-by\n[build-size-url]: https://bundlephobia.com/result?p=index-array-by\n[npm-downloads-img]: https://img.shields.io/npm/dt/index-array-by\n[npm-downloads-url]: https://www.npmtrends.com/index-array-by\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvasturiano%2Findex-array-by","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvasturiano%2Findex-array-by","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvasturiano%2Findex-array-by/lists"}