2018-10-04 14:37:37 +01:00
---
title: Hash from two arrays
id: 595671d4d2cdc305f0d5b36f
challengeType: 5
---
## Description
< section id = 'description' >
2019-03-05 18:37:06 +09:00
Using two Arrays of equal length, create a Hash object where the elements from one array (the keys) are linked to the elements of the other (the values).
2019-06-14 20:04:16 +09:00
< strong > Related task:< / strong >
2019-03-05 18:37:06 +09:00
< ul >
2019-05-22 23:30:29 +09:00
< li > < a href = "https://rosettacode.org/wiki/Associative arrays/Creation" title = "Associative arrays/Creation" target = "_blank" > Associative arrays/Creation< / a > < / li >
2019-03-05 18:37:06 +09:00
< / ul >
2018-10-04 14:37:37 +01:00
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
- text: < code > arrToObj</ code > is a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof arrToObj === 'function');
2018-10-04 14:37:37 +01:00
- text: '< code > arrToObj([1, 2, 3, 4, 5], ["a", "b", "c", "d", "e"])</ code > should return < code > { 1: "a", 2: "b", 3: "c", 4: "d", 5: "e" }</ code > '
2019-07-24 05:09:23 -07:00
testString: assert.deepEqual(arrToObj(...testCases[0]), res[0]);
2018-10-04 14:37:37 +01:00
- text: '< code > arrToObj([1, 2, 3, 4, 5], ["a", "b", "c", "d"])</ code > should return < code > { 1: "a", 2: "b", 3: "c", 4: "d", 5: undefined }</ code > '
2019-07-24 05:09:23 -07:00
testString: assert.deepEqual(arrToObj(...testCases[1]), res[1]);
2018-10-04 14:37:37 +01:00
- text: '< code > arrToObj([1, 2, 3], ["a", "b", "c", "d", "e"])</ code > should return < code > { 1: "a", 2: "b", 3: "c" }</ code > '
2019-07-24 05:09:23 -07:00
testString: assert.deepEqual(arrToObj(...testCases[2]), res[2]);
2018-10-04 14:37:37 +01:00
- text: '< code > arrToObj(["a", "b", "c", "d", "e"], [1, 2, 3, 4, 5])</ code > should return < code > { "a": 1, "b": 2, "c": 3 , "d": 4, "e": 5 }</ code > '
2019-07-24 05:09:23 -07:00
testString: assert.deepEqual(arrToObj(...testCases[3]), res[3]);
2018-10-04 14:37:37 +01:00
- text: '< code > arrToObj(["a", "b", "c", "d", "e"], [1, 2, 3, 4])</ code > should return < code > { "a": 1, "b": 2, "c": 3 , "d": 4, "e": undefined }</ code > '
2019-07-24 05:09:23 -07:00
testString: assert.deepEqual(arrToObj(...testCases[4]), res[4]);
2018-10-04 14:37:37 +01:00
- text: '< code > arrToObj(["a", "b", "c"], [1, 2, 3, 4, 5])</ code > should return < code > { "a": 1, "b": 2, "c": 3 }</ code > '
2019-07-24 05:09:23 -07:00
testString: assert.deepEqual(arrToObj(...testCases[5]), res[5]);
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function arrToObj (keys, vals) {
// Good luck!
return true;
}
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
const testCases = [
[[1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e']],
[[1, 2, 3, 4, 5], ['a', 'b', 'c', 'd']],
[[1, 2, 3], ['a', 'b', 'c', 'd', 'e']],
[['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4, 5]],
[['a', 'b', 'c', 'd', 'e'], [1, 2, 3, 4]],
[['a', 'b', 'c'], [1, 2, 3, 4, 5]]
];
const res = [
{ 1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e' },
{ 1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: undefined },
{ 1: 'a', 2: 'b', 3: 'c' },
{ a: 1, b: 2, c: 3, d: 4, e: 5 },
{ a: 1, b: 2, c: 3, d: 4, e: undefined },
{ a: 1, b: 2, c: 3 }
];
2018-10-04 14:37:37 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function arrToObj (keys, vals) {
return keys.reduce((map, key, index) => {
map[key] = vals[index];
return map;
}, {});
}
```
< / section >