[英]How to double these numbers in an array using recursion, rest/spread operators and destructuring?
I recently completed a JavaScript challenge asking to return a new array with all the values of the initial array doubled.我最近完成了一个 JavaScript 挑战,要求返回一个新数组,其中初始数组的所有值都加倍。
const numbers = [1, 2, 3];
function double() {
}
Except that I was to include some ES6 topics of de-structuring and rest/spread operators as well as recursion.除了我要包含一些关于解构和休息/扩展运算符以及递归的 ES6 主题。 Well, I completed as best as I could to come to a solution.
好吧,我尽我所能完成了一个解决方案。 This was my solution:
这是我的解决方案:
const numbers = [1, 2, 3];
function double(arr){
const doubledNumbers = [];
for (var i = 0; i < arr.length; i ++){
const dubba = arr[i];
const bubba = dubba * 2;
doubledNumbers.push(bubba);
}
return doubledNumbers;
}
The other requirement was to not use any array helper method (map, reduce etc) , so I did not use map()
, but instead a for
loop.另一个要求是不使用任何数组辅助方法(map、reduce 等) ,所以我没有使用
map()
,而是使用for
循环。 However, I could not wrap my head around implementing de-structuring or rest/spread operators, concepts I thought I knew pretty well, never-mind recursion.然而,我无法围绕实现解构或休息/扩展运算符,我认为我非常熟悉的概念,更不用说递归。
Here's one possible implementation - destructure the parameter of double
, taking out the first number in the array, and use rest syntax to put the rest of the numbers into another array.这是一个可能的实现 - 解构
double
的参数,取出数组中的第一个数字,并使用 rest 语法将其余数字放入另一个数组中。 Then, double
the rest of the array, and spread it into a new (returned) array, headed by the first number times 2:然后,将数组的其余部分
double
,并将其扩展到一个新的(返回的)数组中,以第一个数字乘以 2 为首:
const numbers = [1, 2, 3]; function double([firstNum, ...rest]) { const restDoubled = rest.length ? double(rest) : []; return [firstNum * 2, ...restDoubled]; } console.log(double(numbers));
Here's a few alternatives -这里有几个选择——
const None = Symbol("None") const double = ([ x = None, ...more ]) => x === None ? [] : [ x * 2, ...double(more) ] console.log(double([ 1, 2, 3, 4 ])) // [ 2, 4, 6, 8 ]
Without destructuring -没有解构——
const double = (nums = [], i = 0) => i >= nums.length ? [] : [ nums[i] * 2, ...double(nums, i + 1) ] console.log(double([ 1, 2, 3, 4 ])) // [ 2, 4, 6, 8 ]
Using higher-order functions (functions that accept or return other functions) -使用高阶函数(接受或返回其他函数的函数) -
const None = Symbol("None") const identity = x => x const map = ([ x = None, ...more ], f = identity) => x === None ? [] : [ f(x), ...map(more, f) ] const double = (nums = []) => map(nums, n => n * 2) console.log(double([ 1, 2, 3, 4 ])) // [ 2, 4, 6, 8 ]
Using continuation passing style -使用延续传递风格 -
const None = Symbol("None") const identity = x => x const double = ([ x = None, ...more ], then = identity) => x === None ? then([]) : double(more, r => then([ x * 2, ...r ])) double([ 1, 2, 3, 4 ], console.log) // [ 2, 4, 6, 8 ]
当我们可以使用如下简单的代码来做到这一点时,为什么要使用递归
numbers = numbers.map(x => 2*x)
My solution is below -我的解决方案如下 -
const numbers = [1, 2, 3];
let finalResults = [];
function double(numbers) {
const [ number, ...rest ] = numbers;
if(number === undefined) {
return finalResults;
} else {
finalResults.push(number*2);
return double([...rest]);
}
}
A another way:另一种方式:
const numbers = [1, 2, 3];
const arr = [];
function double([x, ...some]) {
arr.push(x*2);
if(!some.length)
return arr;
return double(some);
}
double(1,2,3)
will return double(2,3)
which in turn will return double(3)
and finally double(3)
is going to return array [2,4,6]
double(1,2,3)
将返回double(2,3)
,后者又将返回double(3)
,最后double(3)
将返回数组[2,4,6]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.