├── README.md ├── array-to-object-with-property-names-that-map-to-array-values-and-property-values-that-are-objects-with-keys-whose-values-also-map-to-array-values.js ├── package.json └── test.js /README.md: -------------------------------------------------------------------------------- 1 | # array-to-object-with-property-names-that-map-to-array-values-and-property-values-that-are-objects-with-keys-whose-values-also-map-to-array-values 2 | 3 | Convert an array to an object whose property names map to the array items, and whose values are objects whose properties in turn you can specify, and those property values are also mapped to the current array item. 4 | 5 | ## Example 6 | 7 | ```javascript 8 | var arrayToObjectWithPropertyNamesThatMapToArrayValuesAndPropertyValuesThatAreObjectsWithKeysWhoseValuesAlsoMapToArrayValues = require('array-to-object-with-property-names-that-map-to-array-values-and-property-values-that-are-objects-with-keys-whose-values-also-map-to-array-values'); 9 | 10 | arrayToObjectWithPropertyNamesThatMapToArrayValuesAndPropertyValuesThatAreObjectsWithKeysWhoseValuesAlsoMapToArrayValues(['foo', 'bar'], ['name', 'id']); 11 | 12 | // Result: 13 | // { 14 | // foo: {name: 'foo', id: 'foo'}, 15 | // bar: {name: 'bar', id: 'bar'}, 16 | // } 17 | ``` 18 | 19 | ## License 20 | 21 | ISC 22 | -------------------------------------------------------------------------------- /array-to-object-with-property-names-that-map-to-array-values-and-property-values-that-are-objects-with-keys-whose-values-also-map-to-array-values.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function arrayToObjectWithPropertyNamesThatMapToArrayValuesAndPropertyValuesThatAreObjectsWithKeysWhoseValuesAlsoMapToArrayValues(array, keys) { 4 | return array.reduce(function mapArrayToObject(result, item) { 5 | result[item] = keys.reduce(function mapKeysToArrayValues(propObject, key) { 6 | propObject[key] = item; 7 | return propObject; 8 | }, {}) 9 | return result; 10 | }, {}); 11 | } 12 | 13 | module.exports = arrayToObjectWithPropertyNamesThatMapToArrayValuesAndPropertyValuesThatAreObjectsWithKeysWhoseValuesAlsoMapToArrayValues; 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "array-to-object-with-property-names-that-map-to-array-values-and-property-values-that-are-objects-with-keys-whose-values-also-map-to-array-values", 3 | "version": "1.0.0", 4 | "description": "Convert an array of strings to an object whose properties map to the array values, and whose values are objects whose properties in turn you can specify, and those property values are also mapepd to the current array value.", 5 | "main": "array-to-object-with-property-names-that-map-to-array-values-and-property-values-that-are-objects-with-keys-whose-values-also-map-to-array-values.js", 6 | "scripts": { 7 | "test": "node test" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/lxe/array-to-object-with-property-names-that-map-to-array-values-and-property-values-that-are-objects-with-keys-whose-values-also-map-to-array-values.git" 12 | }, 13 | "keywords": [ 14 | "array", 15 | "object", 16 | "reduce", 17 | "map", 18 | "properties", 19 | "convert" 20 | ], 21 | "author": "Aleksey Smolenchuk ", 22 | "license": "ISC", 23 | "bugs": { 24 | "url": "https://github.com/lxe/array-to-object-with-property-names-that-map-to-array-values-and-property-values-that-are-objects-with-keys-whose-values-also-map-to-array-values/issues" 25 | }, 26 | "homepage": "https://github.com/lxe/array-to-object-with-property-names-that-map-to-array-values-and-property-values-that-are-objects-with-keys-whose-values-also-map-to-array-values#readme" 27 | } 28 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('assert'); 4 | var arrayToObjectWithPropertyNamesThatMapToArrayValuesAndPropertyValuesThatAreObjectsWithKeysWhoseValuesAlsoMapToArrayValues = require('./array-to-object-with-property-names-that-map-to-array-values-and-property-values-that-are-objects-with-keys-whose-values-also-map-to-array-values.js'); 5 | 6 | assert.deepEqual( 7 | arrayToObjectWithPropertyNamesThatMapToArrayValuesAndPropertyValuesThatAreObjectsWithKeysWhoseValuesAlsoMapToArrayValues(['foo', 'bar'], ['name', 'id']), 8 | { 9 | foo: {name: 'foo', id: 'foo'}, 10 | bar: {name: 'bar', id: 'bar'}, 11 | }, 12 | 'Shoudl convert an array'); --------------------------------------------------------------------------------