{"id":24367175,"url":"https://github.com/vajahath/array-objectify","last_synced_at":"2026-04-10T23:06:52.546Z","repository":{"id":57184045,"uuid":"83431051","full_name":"vajahath/array-objectify","owner":"vajahath","description":"Hierarchically transform an array of objects into a single object","archived":false,"fork":false,"pushed_at":"2018-02-12T08:39:33.000Z","size":29,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-21T01:38:21.858Z","etag":null,"topics":["array","npm","objects","parser"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/array-objectify","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/vajahath.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}},"created_at":"2017-02-28T12:41:21.000Z","updated_at":"2018-02-12T07:30:12.000Z","dependencies_parsed_at":"2022-08-23T01:20:51.407Z","dependency_job_id":null,"html_url":"https://github.com/vajahath/array-objectify","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Farray-objectify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Farray-objectify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Farray-objectify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Farray-objectify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vajahath","download_url":"https://codeload.github.com/vajahath/array-objectify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243197166,"owners_count":20251944,"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","npm","objects","parser"],"created_at":"2025-01-19T01:42:09.225Z","updated_at":"2025-12-27T00:43:42.283Z","avatar_url":"https://github.com/vajahath.png","language":"JavaScript","funding_links":["https://paypal.me/vajahath"],"categories":[],"sub_categories":[],"readme":"# Array Objectify\r\n\r\nHierarchically transform an array of similar objects into a single object.\r\n\r\n[![npm](https://img.shields.io/npm/v/array-objectify.svg)](https://www.npmjs.com/package/array-objectify) [![Build Status](https://travis-ci.org/vajahath/array-objectify.svg?branch=master)](https://travis-ci.org/vajahath/array-objectify) [![Greenkeeper badge](https://badges.greenkeeper.io/vajahath/array-objectify.svg)](https://greenkeeper.io/) [![npm](https://img.shields.io/npm/dt/array-objectify.svg)](https://www.npmjs.com/package/array-objectify)\r\n\r\n![](https://raw.githubusercontent.com/vajahath/array-objectify/master/media/highlight.png)\r\n\r\n## Install / Update\r\n\r\n```bash\r\nnpm install --save array-objectify\r\n```\r\n\r\n### Syntax\r\n\r\n```\r\noutput = arrayObjectify(hierarchy, data[, keepRepetetions])\r\n```\r\n\r\n* `hierarchy` is an `array`: Determines the order of parsing.\r\n* `data` is an `obect`: Defines the data we want to transform.\r\n* `keepRepetetions` is `boolean` (optional, default value is `false`): If this flag is set, child arrays of the result will contain the exact values gotten from the input data without checking for duplication. (look at the example)\r\n\r\n## Usage\r\n\r\n```javascript\r\nconst arrayObjectify = require('array-objectify');\r\n\r\n// this is the data we want to parse into new object format\r\nconst data = [\r\n\t{\r\n\t\tfruit: 'apple',\r\n\t\tcolor: 'yellow',\r\n\t\tcontent: 'Vitamin Y',\r\n\t\tnickName: 'Y-diple'\r\n\t},\r\n\t{\r\n\t\tfruit: 'apple',\r\n\t\tcolor: 'yellow',\r\n\t\tcontent: 'Vitamin X',\r\n\t\tnickName: 'X-diple'\r\n\t},\r\n\t{\r\n\t\tfruit: 'mango',\r\n\t\tcolor: 'red',\r\n\t\tcontent: 'Vitamin yellow',\r\n\t\tnickName: 'yellow-mango'\r\n\t},\r\n\t{\r\n\t\tfruit: 'apple',\r\n\t\tcolor: 'green',\r\n\t\tcontent: 'Vitamin green',\r\n\t\tnickName: 'green-mango'\r\n\t}\r\n];\r\n\r\n// now we've to mention the order of hierarchy in which array-objectify should parse the data\r\nconst hierarchy = ['fruit', 'color'];\r\n\r\n// parse data in the order of given hierarchy\r\nconst objectified = arrayObjectify(hierarchy, data);\r\nconsole.log(objectified);\r\n/* output=\u003e\r\n\r\n{\r\n\t\"apple\": {\r\n\t\t\"yellow\": {\r\n\t\t\t\"content\": [\r\n\t\t\t\t\"Vitamin Y\",\r\n\t\t\t\t\"Vitamin X\"\r\n\t\t\t],\r\n\t\t\t\"nickName\": [\r\n\t\t\t\t\"Y-diple\",\r\n\t\t\t\t\"X-diple\"\r\n\t\t\t]\r\n\t\t},\r\n\t\t\"green\": {\r\n\t\t\t\"content\": [\r\n\t\t\t\t\"Vitamin green\"\r\n\t\t\t],\r\n\t\t\t\"nickName\": [\r\n\t\t\t\t\"green-mango\"\r\n\t\t\t]\r\n\t\t}\r\n\t},\r\n\t\"mango\": {\r\n\t\t\"red\": {\r\n\t\t\t\"content\": [\r\n\t\t\t\t\"Vitamin yellow\"\r\n\t\t\t],\r\n\t\t\t\"nickName\": [\r\n\t\t\t\t\"yellow-mango\"\r\n\t\t\t]\r\n\t\t}\r\n\t}\r\n}\r\n\r\n*/\r\n```\r\n\r\n## Things Explained\r\n\r\n* We're passing an array of **similar objects** and we want that to be **hierarchically** parsed into an object.\r\n* The syntax is `output = arrayObjectify(hierarchy, data, keepRepetetions)`.\r\n    * `data` is the `array` of `objects` we want to convert.\r\n    * `hierarchy` is an `array` of hierarchically ordered object field names in which the package should parse the data.\r\n    * `keepRepetetions` is `Boolean` and this argument is optional. Default value is `false`.\r\n    * The field names that are not mentioned in the `hierarchy` will be the child of the last parent. Consider the below example:\r\n\r\n```js\r\nconst data = [\r\n\t{\r\n\t\tfruit: 'apple',\r\n\t\tcolor: 'yellow',\r\n\t\tcontent: 'Vitamin Y',\r\n\t\tnickName: 'Y-diple'\r\n\t},\r\n\t{\r\n\t\tfruit: 'apple',\r\n\t\tcolor: 'yellow',\r\n\t\tcontent: 'Vitamin X',\r\n\t\tnickName: 'X-diple'\r\n\t},\r\n\t{\r\n\t\tfruit: 'mango',\r\n\t\tcolor: 'red',\r\n\t\tcontent: 'Vitamin yellow',\r\n\t\tnickName: 'yellow-mango'\r\n\t},\r\n\t{\r\n\t\tfruit: 'mango',\r\n\t\tcolor: 'red',\r\n\t\tcontent: 'Vitamin yellow',\r\n\t\tnickName: 'yellow-mango-dupe'\r\n\t},\r\n\t{\r\n\t\tfruit: 'apple',\r\n\t\tcolor: 'green',\r\n\t\tcontent: 'Vitamin green',\r\n\t\tnickName: 'green-mango'\r\n\t}\r\n];\r\n```\r\n\r\nLet's say we want to covert this whole array into an object, based on the field name `fruit`. And then each fruit need to be again ordered based on the field name `color`. Rest of the fields names (`content` and `nickName`) are local to each colored fruit. **So we should not want them to mention it on `hierarchy`**.\r\nThus the hierarchy goes like:\r\n\r\n```js\r\nconst hierarchy = ['fruit', 'color'];\r\n```\r\n\r\nThose fields which are not mentioned in the `hierarchy` will come under the last hierarchical parent, in our case, the `color` field.\r\nNow the parsed output would be as follows:\r\n\r\n```js\r\nconst out = arrayObjectify(hierarchy, data); // keepRepetetions = fasle (default)\r\nconsole.log(out);\r\n```\r\n\r\noutput =\u003e\r\n\r\n```js\r\n{\r\n\t\"apple\": {\r\n\t\t\"yellow\": {\r\n\t\t\t\"content\": [\"Vitamin Y\",\"Vitamin X\"],\r\n\t\t\t\"nickName\": [\"Y-diple\",\"X-diple\"]\r\n\t\t},\r\n\t\t\"green\": {\r\n\t\t\t\"content\": [\"Vitamin green\"],\r\n\t\t\t\"nickName\": [\"green-mango\"]\r\n\t\t}\r\n\t},\r\n\t\"mango\": {\r\n\t\t\"red\": {\r\n\t\t\t\"content\": [\"Vitamin yellow\"],\r\n\t\t\t\"nickName\": [\"yellow-mango\",\"yellow-mango-dupe\"]\r\n\t\t}\r\n\t}\r\n}\r\n```\r\n\r\nLet's run this again with `keepRepetetions` turned on, so that you can visually differentiate the action of `keepRepetetions` flag:\r\n\r\n```js\r\nlet out = arrayObjectify(hierarchy, data, true);\r\nconsole.log(out);\r\n```\r\n\r\noutput =\u003e\r\n\r\n```js\r\n{\r\n\t\"apple\": {\r\n\t\t\"yellow\": {\r\n\t\t\t\"content\": [\"Vitamin Y\",\"Vitamin X\"],\r\n\t\t\t\"nickName\": [\"Y-diple\",\"X-diple\"]\r\n\t\t},\r\n\t\t\"green\": {\r\n\t\t\t\"content\": [\"Vitamin green\"],\r\n\t\t\t\"nickName\": [\"green-mango\"]\r\n\t\t}\r\n\t},\r\n\t\"mango\": {\r\n\t\t\"red\": {\r\n\t\t\t\"content\": [\"Vitamin yellow\",\"Vitamin yellow\"],\r\n\t\t\t\"nickName\": [\"yellow-mango\",\"yellow-mango-dupe\"]\r\n\t\t}\r\n\t}\r\n}\r\n```\r\n\r\nLook at the `mango` field: `Vitamin yellow` is repeating. In some usecases, it may requre things in that way. It will not repeat if `keepRepetetions` is turned off.\r\n\r\n---\r\n\r\n_Still unclear? Please rise an [issue](https://github.com/vajahath/array-objectify/issues)._\r\n\r\nEnjoy :)\r\n\r\n\u003e Just in case, if you liked this package, [![PayPal][badge_paypal_donate]][paypal-donations]\r\n\r\n## Change log\r\n\r\n* **v1.0.1, 1.0.2, 1.0.3**\r\n    * Better docs - Handling greenkeeper issues\r\n* **v1.0.0**\r\n    * Initial release\r\n\r\n## License\r\n\r\nMIT \u0026copy; [Vajahath Ahmed](https://mycolorpad.blogspot.in)\r\n\r\n[badge_paypal_donate]: https://cdn.rawgit.com/vajahath/cloud-codes/a01f087f/badges/paypal_donate.svg\r\n[paypal-donations]: https://paypal.me/vajahath\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvajahath%2Farray-objectify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvajahath%2Farray-objectify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvajahath%2Farray-objectify/lists"}