{"id":19964375,"url":"https://github.com/arve0/deep-reduce","last_synced_at":"2025-10-20T10:20:42.637Z","repository":{"id":66164236,"uuid":"94128512","full_name":"arve0/deep-reduce","owner":"arve0","description":"reduce objects deeply, call reducer for every nested value in object tree","archived":false,"fork":false,"pushed_at":"2017-10-10T05:49:18.000Z","size":26,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-19T18:53:41.205Z","etag":null,"topics":["array","deeply-nested","nested-values","object","reduce","values"],"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/arve0.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-12T18:36:55.000Z","updated_at":"2022-08-03T23:40:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"696bb35c-8c16-41ae-b0ed-e73dd9ba40cd","html_url":"https://github.com/arve0/deep-reduce","commit_stats":{"total_commits":21,"total_committers":1,"mean_commits":21.0,"dds":0.0,"last_synced_commit":"375e1058b61671656dc602b2b58d742fe9dd95a2"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arve0%2Fdeep-reduce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arve0%2Fdeep-reduce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arve0%2Fdeep-reduce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arve0%2Fdeep-reduce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arve0","download_url":"https://codeload.github.com/arve0/deep-reduce/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252269026,"owners_count":21721239,"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","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","deeply-nested","nested-values","object","reduce","values"],"created_at":"2024-11-13T02:23:18.258Z","updated_at":"2025-10-20T10:20:42.509Z","avatar_url":"https://github.com/arve0.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/arve0/deep-reduce.svg?branch=master)](https://travis-ci.org/arve0/deep-reduce) [![npm version](https://badge.fury.io/js/deep-reduce.svg)](https://badge.fury.io/js/deep-reduce)\n# deep-reduce\nReduce objects deeply, call reducer for every nested node in object tree. Use `deepReduce` for\ntransforming/getting values from awfully nested objects. `deepReduce` also traverse arrays.\n\n\n## Install\n```sh\nnpm i deep-reduce\n```\n\n\n## Usage\n[Try example in browser](https://runkit.com/npm/deep-reduce).\n\n```js\nconst deepReduce = require('deep-reduce')\nconst deepEqual = require('assert').deepEqual\n\n// let nested leaf values be equal to their path, for demonstration purpose\nlet deepNestedObject = {\n  a: 'a',\n  b: { c: 'b.c' },\n  c: [\n    'c.0',\n    {\n      d: 'c.1.d',\n      e: [\n        'c.1.e.0'\n      ]\n    }\n  ]\n}\n\n// store all values which are strings to reduced[path].\nlet flattenStrings = (reduced, value, path) =\u003e {\n  if (typeof value === 'string') {\n    reduced[path] = value\n  }\n  return reduced\n}\n\n// the reduced value is returned\nlet reduced = deepReduce(deepNestedObject, flattenStrings)\n\n// we should now have this object\ndeepEqual(reduced, {\n  'a': 'a',\n  'b.c': 'b.c',\n  'c.0': 'c.0',\n  'c.1.d': 'c.1.d',\n  'c.1.e.0': 'c.1.e.0'\n})\n```\n\nRoot object may be an array also:\n```js\ndeepReduce([{a: 1},{b: 2},{a: 3}], (r,v) =\u003e typeof v === 'number' ? r + v : r, 0)\n// 6\n```\n\nHere is how you would collect all items of nested arrays at some specific path:\n```js\n// we want to get contents from all packets\nlet transport = {\n  id: 'A8811',\n  packages: [\n    {\n      id: 'P100',\n      contents: [\n        {\n          id: 'R88',\n          name: 'resistor'\n        },\n        {\n          id: 'C99',\n          name: 'capacitor'\n        }\n      ]\n    }, {\n      id: 'P101',\n      contents: [\n        {\n          id: 'C96',\n          name: 'coil'\n        }\n      ]\n    }\n  ]\n}\n\nlet contents = deepReduce(transport, (reduced, value, path) =\u003e {\n  if (path.match(/packages\\.\\d+\\.contents\\.\\d+$/)) {\n    // path is packages.n.contents.m\n    // item n in packages array\n    // item m in contents array\n    reduced.push(value)\n  }\n  return reduced\n}, [])  // start with an empty array\n\n// [ { id: 'R88', name: 'resistor' },\n//   { id: 'C99', name: 'capacitor' },\n//   { id: 'C96', name: 'coil' } ]\n```\n\n\n## API\n`deepReduce` takes 5 arguments. 2 mandatory and 3 optional:\n\n```ts\ndeepReduce (\n  obj: object,\n  reducer: (reduced: any, value: any, path: string, root: object) =\u003e any,\n  reduced = {},\n  path = '',\n  thisArg = {}): any\n```\n\n### Arguments\n- `obj` Object to traverse.\n- `reducer` Function to call with every value in `obj`-tree. See section below\n  for [`reducer` function signature](#arguments-for-reducer-function).\n- `reduced` Initial value of `reduced` passed to `reducer`. Defaults to empty object `{}`.\n- `path` Path to root, start traversing here. Nice to omit looping through parts of `obj`.\n\n  Example:\n  ```js\n  deepReduce({ a: [1,2,3], b: { c: [3, 3] } }, (reduced, val) =\u003e reduced + val, 1, 'b.c')\n  // only traverses b.c, returns 1 + 3 + 3 = 7\n  ```\n\n- `thisArg` Bound to reducer as `this`.\n\n### Arguments for reducer function\nThe reducer function is called with these arguments:\n\n```ts\n(reduced: any, value: any, path: string, root: object) =\u003e any\n```\n\n- `reduced` Initial or current reduced value.\n- `value` Value of current node.\n- `path` Path to current value.\n- `root` Root object passed to `deepReduce` as `obj`.\n\nThe `reducer` should return the `reduced` value.\n\n\n## Performance\n`deep-reduce` traverses every node of a 149 kb JSON in 10 milliseconds on a macbook air 2013 i5 1.3GHz, see [test.js](test.js#L68-L73).\n\nYou can omit traversal of parts of the object tree with defining a start `path`:\n\n```js\ndeepReduce(object, reducer, initialValue, 'start.at.this.path')\n```\n\n...which may give some performance gains.\n\nIf you want to use this client size, bundle size is ~ 387 B, 209 B gzipped.\n\n## Development\n```sh\ngit clone https://github.com/arve0/deep-reduce\ncd deep-reduce\nnpm start  # watches index.ts and builds on any change\nnpm test  # runs node test.js\n```\n\n## License\nMIT © 2017 Arve Seljebu\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farve0%2Fdeep-reduce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farve0%2Fdeep-reduce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farve0%2Fdeep-reduce/lists"}