Created
September 17, 2017 20:52
-
-
Save batogov/564dbcc8e8cf4701f0f4115e8ed22238 to your computer and use it in GitHub Desktop.
Map and reduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1, 2, 3, 4]; | |
/* | |
Реализовать map через reduce. | |
Внимание! Если initialValue передано, то на первой итерации функция будет вызвана с | |
этим значением и значением первого элемента массива. Если же initialValue не передано, | |
то функция будет вызвана со значениями первого и второго элементов массива. Отсюда также | |
следует, что если начальное значение не передано, то функция вызывается на один раз меньше, | |
иначе ровно столько раз, сколько элементов в массиве. | |
*/ | |
result = []; | |
a.reduce(function(accum, curr, index, arr) { | |
result[index] = curr + 1; | |
}, 0); | |
/* | |
Реализовать reduce через map. | |
*/ | |
accum = []; | |
a.map(function(curr, index, arr) { | |
accum += curr; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment