2018-10-10 18:03:03 -04:00
---
title: Hash from two arrays
id: 595671d4d2cdc305f0d5b36f
challengeType: 5
2019-08-28 16:26:13 +03:00
forumTopicId: 302283
2018-10-10 18:03:03 -04:00
localeTitle: Хэш из двух массивов
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
Задача: < p > Используя два массива равной длины, создайте объект Hash, в котором элементы из одного массива (ключи) связаны с элементами другого (значения) < / p > Связанная задача: < a href = "http://rosettacode.org/wiki/Associative arrays/Creation" title = "Ассоциативные массивы / Создание" > Ассоциативные массивы / Создание< / a >
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
2018-10-10 18:03:03 -04:00
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: < code > arrToObj</ code > is a function.
testString: assert(typeof arrToObj === 'function');
- 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 > '
testString: assert.deepEqual(arrToObj(...testCases[0]), res[0]);
- 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 > '
testString: assert.deepEqual(arrToObj(...testCases[1]), res[1]);
- text: '< code > arrToObj([1, 2, 3], ["a", "b", "c", "d", "e"])</ code > should return < code > { 1: "a", 2: "b", 3: "c" }</ code > '
testString: assert.deepEqual(arrToObj(...testCases[2]), res[2]);
- 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 > '
testString: assert.deepEqual(arrToObj(...testCases[3]), res[3]);
- 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 > '
testString: assert.deepEqual(arrToObj(...testCases[4]), res[4]);
- text: '< code > arrToObj(["a", "b", "c"], [1, 2, 3, 4, 5])</ code > should return < code > { "a": 1, "b": 2, "c": 3 }</ code > '
testString: assert.deepEqual(arrToObj(...testCases[5]), res[5]);
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function arrToObj (keys, vals) {
// Good luck!
return true;
}
```
< / div >
2019-08-28 16:26:13 +03:00
### After Tests
2018-10-10 18:03:03 -04:00
< div id = 'js-teardown' >
```js
2019-08-28 16:26:13 +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-10 18:03:03 -04:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
function arrToObj (keys, vals) {
return keys.reduce((map, key, index) => {
map[key] = vals[index];
return map;
}, {});
}
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >