{"id":21567527,"url":"https://github.com/alcidesqueiroz/obrray","last_synced_at":"2025-03-18T05:41:15.258Z","repository":{"id":55928004,"uuid":"123763078","full_name":"alcidesqueiroz/obrray","owner":"alcidesqueiroz","description":"✨Convert between objects and arrays in many different ways. Wololoooo!","archived":false,"fork":false,"pushed_at":"2020-12-06T23:59:10.000Z","size":103,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-11-20T15:22:26.066Z","etag":null,"topics":["arrays","conversion","mapping","objects"],"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/alcidesqueiroz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-04T06:24:21.000Z","updated_at":"2020-12-06T17:45:42.000Z","dependencies_parsed_at":"2022-08-15T09:40:27.183Z","dependency_job_id":null,"html_url":"https://github.com/alcidesqueiroz/obrray","commit_stats":null,"previous_names":[],"tags_count":4,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcidesqueiroz%2Fobrray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcidesqueiroz%2Fobrray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcidesqueiroz%2Fobrray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alcidesqueiroz%2Fobrray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alcidesqueiroz","download_url":"https://codeload.github.com/alcidesqueiroz/obrray/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244166637,"owners_count":20409177,"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":["arrays","conversion","mapping","objects"],"created_at":"2024-11-24T10:31:28.490Z","updated_at":"2025-03-18T05:41:15.228Z","avatar_url":"https://github.com/alcidesqueiroz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# obrray [![Build status](https://travis-ci.com/alcidesqueiroz/obrray.svg?branch=master)](https://travis-ci.com/alcidesqueiroz/obrray)\n\n\u003e Convert between objects and arrays in many different ways. Wololoooo!\n\n![Wololoooo!](https://gist.githubusercontent.com/alcidesqueiroz/c3d6c6edc559194bc37a2c464a21768d/raw/9edd7783c178fb4e1f0355047fa5e2778b0fc3bc/wololo.png)\n\n## Install\n\nWith npm:\n```\n$ npm install obrray\n```\n\nWith Yarn:\n\n```\n$ yarn add obrray\n```\n\n## Usage\n\n### toObject(arr[, options])\n\n#### arr\n\nType: `Array`\n\nArray to be converted.\n\n\n```js\n\nconst obrray = require('obrray');\n\nobrray.toObject([]);\n//=\u003e {}\n\nobrray.toObject([123]);\n//=\u003e { 0: 123 }\n\nobrray.toObject([11, 22, 33]);\n//=\u003e { 0: 11, 1: 22, 2: 33 }\n\nobrray.toObject([[11, 22], [33, 44], [55, 66]]);\n//=\u003e { 0: [11, 22], 1: [33, 44], 2: [55, 66] }\n```\n\n#### options\n\nType: `Object`\n\n##### sameKeyAndValue\n\nType: `boolean`\n\nUses each array item both as key and value of the target object.\n\n\n```js\nobrray.toObject([11, 22, 33], { sameKeyAndValue: true });\n//=\u003e { 11: 11, 22: 22, 33: 33 }\n```\n\n##### keyAndValuePairs\n\nType: `boolean|object`\n\nConverts from a bidimensional array with key-value pairs.\n\n```js\n// Standard key-value pairs array\nobrray.toObject(\n  [\n    ['name', 'Ford Prefect'],\n    ['planet', 'Betelgeuse Seven'],\n    ['nickname', 'lx']\n  ], { keyAndValuePairs: true });\n//=\u003e {\n//     name: 'Ford Prefect',\n//     planet: 'Betelgeuse Seven',\n//     nickname: 'lx'\n//   }\n\n\n// Array with custom indexes for key and value\nobrray.toObject(\n  [\n    [147, false, 'Ford Prefect', [], 'name'],\n    [215, true, 'Betelgeuse Seven', [111], 'planet'],\n    [12, false, 'lx', [27, 1], 'nickname']\n  ], { keyAndValuePairs: { keyIndex: 4, valueIndex: 2 } });\n//=\u003e {\n//     name: 'Ford Prefect',\n//     planet: 'Betelgeuse Seven',\n//     nickname: 'lx'\n//   }\n```\n\n##### invertedKeyAndValuePairs\n\nType: `boolean`\n\nConverts from a bidimensional array with inverted key-value pairs.\n\n```js\n\n// Inverted key-value pairs array\nobrray.toObject(\n  [\n    ['Ford Prefect', 'name'],\n    ['Betelgeuse Seven', 'planet'],\n    ['lx', 'nickname']\n  ], { invertedKeyAndValuePairs: true });\n//=\u003e {\n//     name: 'Ford Prefect',\n//     planet: 'Betelgeuse Seven',\n//     nickname: 'lx'\n//   }\n```\n\n##### keyAndValueObjects\n\nType: `boolean|object`\n\nConverts from an array of objects with key and value properties.\n\n```js\n// Key-value properties with default names\nobrray.toObject(\n  [\n    { key: 'name', value: 'Ford Prefect' },\n    { key: 'planet', value: 'Betelgeuse Seven' },\n    { key: 'nickname', value: 'lx' }\n  ], { keyAndValueObjects: true });\n//=\u003e {\n//     name: 'Ford Prefect',\n//     planet: 'Betelgeuse Seven',\n//     nickname: 'lx'\n//   }\n\n\n// Key-value properties with custom names\nobrray.toObject(\n  [\n    { k: 'name', v: 'Ford Prefect' },\n    { k: 'planet', v: 'Betelgeuse Seven' },\n    { k: 'nickname', v: 'lx' }\n  ], { keyAndValueObjects: { keyProperty: 'k', valueProperty: 'v' } });\n//=\u003e {\n//     name: 'Ford Prefect',\n//     planet: 'Betelgeuse Seven',\n//     nickname: 'lx'\n//   }\n```\n\n##### mapper\n\nType: `function`\n\nUses a callback function to map each array item in a custom way.\n\n```js\n// Default approach, by returning the new value from the callback\nobrray.toObject(\n  [\n    ['name', 147, 27, 42, false, 15, 'Ford Prefect', false],\n    ['planet', 'Betelgeuse Seven', [111]],\n    ['nickname', 12, false, 'lx', [27, 1], 147, 98]\n  ], {\n    mapper: (item) =\u003e {\n      for (let i = 1; i \u003c item.length; i++) {\n        if (typeof item[i] !== 'string') continue;\n\n        return { key: item[0], value: item[i] };\n      }\n    }\n  });\n//=\u003e {\n//     name: 'Ford Prefect',\n//     planet: 'Betelgeuse Seven',\n//     nickname: 'lx'\n//   }\n\n// Changing the target object from within the callback\nobrray.toObject(\n  [\n    ['name', 147, 27, 42, false, 15, 'Ford Prefect', false],\n    ['planet', 'Betelgeuse Seven', [111]],\n    ['nickname', 12, false, 'lx', [27, 1], 147, 98]\n  ], {\n    mapper: (item, targetObj) =\u003e {\n      for (let i = 1; i \u003c item.length; i++) {\n        if (typeof item[i] !== 'string') continue;\n\n        targetObj[item[0]] = item[i];\n        break;\n      }\n    }\n  });\n//=\u003e {\n//     name: 'Ford Prefect',\n//     planet: 'Betelgeuse Seven',\n//     nickname: 'lx'\n//   }\n```\n\n\n### toArray(obj[, options])\n\n#### obj\n\nType: `Object`\n\nObject to be converted.\n\n```js\nobrray.toArray({});\n//=\u003e []\n\nobrray.toArray({ whatever: 123 });\n//=\u003e [123]\n\nobrray.toArray({ name: 'Vito', surname: 'Corleone' });\n//=\u003e ['Vito', 'Corleone']\n\nobrray.toArray({ 2: 'Corleone', 1: 'Andolini', 0: 'Vito' });\n//=\u003e ['Vito', 'Andolini', 'Corleone']\n\nobrray.toArray({ foo: 'bar', 2: 'Corleone', 1: 'Andolini', 0: 'Vito', bar: 'foo' });\n//=\u003e ['Vito', 'Andolini', 'Corleone', 'bar', 'foo']\n```\n\n##### useKeys\n\nType: `boolean`\n\nUses property keys instead of values for each item of the mapped array\n\n```js\nobrray.toArray({ name: 'Vito', surname: 'Corleone' }, { useKeys: true });\n//=\u003e ['name', 'surname']\n```\n\n##### toKeyAndValuePairs\n\nType: `boolean`\n\nConverts to an array of key and value pairs\n\n```js\nobrray.toArray(\n  {\n    name: 'Vito',\n    middleName: 'Andolini',\n    surname: 'Corleone'\n  }, { toKeyAndValuePairs: true });\n//=\u003e [['name', 'Vito'], ['middleName', 'Andolini'], ['surname', 'Corleone']]\n```\n\n##### toInvertedKeyAndValuePairs\n\nType: `boolean`\n\nConverts to an array of inverted key and value pairs\n\n```js\nobrray.toArray(\n  {\n    name: 'Vito',\n    middleName: 'Andolini',\n    surname: 'Corleone'\n  }, { toInvertedKeyAndValuePairs: true });\n//=\u003e [['Vito', 'name'], ['Andolini', 'middleName'], ['Corleone', 'surname']]\n```\n\n##### toKeyAndValueObjects\n\nType: `boolean|object`\n\nConverts to an array of key and value objects\n\n```js\n// Using default key-value property names\nobrray.toArray(\n  {\n    name: 'Vito',\n    middleName: 'Andolini',\n    surname: 'Corleone'\n  }, { toKeyAndValueObjects: true });\n//=\u003e [{ key: 'name', value: 'Vito' }, { key: 'middleName', value: 'Andolini' }, { key: 'surname', value: 'Corleone' }]\n\n// Using custom key-value property names\nobrray.toArray(\n  {\n    name: 'Vito',\n    middleName: 'Andolini',\n    surname: 'Corleone'\n  }, { toKeyAndValueObjects: { keyProperty: 'k', valueProperty: 'v' } });\n//=\u003e  [{ k: 'name', v: 'Vito' }, { k: 'middleName', v: 'Andolini' }, { k: 'surname', v: 'Corleone' }]\n```\n\n##### sorter\n\nType: `function`\n\nSorts the output array\n\n```js\nobrray.toArray(\n  {\n    name: 'Vito',\n    middleName: 'Andolini',\n    surname: 'Corleone'\n  }, { sorter: (a, b) =\u003e a.value.localeCompare(b.value) });\n//=\u003e  ['Andolini', 'Corleone', 'Vito']\n\n// Using a sorter along with other option\nobrray.toArray(\n  {\n    name: 'Vito',\n    middleName: 'Andolini',\n    surname: 'Corleone'\n  }, { sorter: (a, b) =\u003e a.value.localeCompare(b.value), toKeyAndValuePairs: true });\n//=\u003e  [['middleName', 'Andolini'], ['surname', 'Corleone'], ['name', 'Vito']]\n```\n\n##### mapper\n\nType: `function`\n\nUses a callback function to map each object property in a custom way.\n\n```js\n// Default approach, by returning the new value from the callback\nobrray.toArray(\n  {\n    name: 'Vito',\n    middleName: 'Andolini',\n    surname: 'Corleone'\n  }, { mapper: (item) =\u003e `The ${item.key} is \"${item.value}\"` });\n//=\u003e  ['The name is \"Vito\"', 'The middleName is \"Andolini\"', 'The surname is \"Corleone\"']\n\n// Changing the target array from within the callback\nobrray.toArray(\n  {\n    name: 'Vito',\n    middleName: 'Andolini',\n    surname: 'Corleone'\n  },\n  {\n    mapper: (item, targetArr) =\u003e {\n      targetArr.push(`The ${item.key} is \"${item.value}\"`);\n      targetArr.push('...an additional item...');\n    }\n  });\n//=\u003e  [\n//      'The name is \"Vito\"',\n//      '...an additional item...',\n//      'The middleName is \"Andolini\"',\n//      '...an additional item...',\n//      'The surname is \"Corleone\"',\n//      '...an additional item...'\n//    ]\n```\n\n## Author\n\nAlcides Queiroz Aguiar\n\n- Website: [www.alcidesqueiroz.com](https://www.alcidesqueiroz.com)\n- Medium: [@alcidesqueiroz](https://medium.com/@alcidesqueiroz)\n- Twitter: [alcidesqueiroz](https://twitter.com/alcidesqueiroz)\n- Behance [alcidesqueiroz](https://behance.net/alcidesqueiroz)\n- Stack Overflow: [http://is.gd/aqanso](http://stackoverflow.com/users/1295666/alcides-queiroz-aguiar)\n- E-mail: alcidesqueiroz \u0026lt;at\u0026gt; gmail\n\n## License\n\nThis code is free to use under the terms of the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falcidesqueiroz%2Fobrray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falcidesqueiroz%2Fobrray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falcidesqueiroz%2Fobrray/lists"}