简体   繁体   English

使用递归在数组中添加数字,而忽略任何其他数据类型

[英]Add numbers in Array using recursion and disregarding any other data type

Forgive me if this is to basic or have been asked already, but I'm stuck at this problem and I feel like it must be something simple I'm not seeing. 原谅我这是基本问题还是已经被问到了,但是我陷入了这个问题,我觉得这一定是我没看到的简单问题。

I want to add all numbers in the array using recursion(done!) and I'm missing a statement to ignore all other types of values. 我想使用recursion(done!)将数组中的所有数字相加,但我缺少一条语句来忽略所有其他类型的值。 For example: 例如:

var arr = [1,'a','b',2,[1],'c']
sumValues(arr) // => 4 . 

function sumValues(arr){
  if(arr.length === 0){
    return 0;
  } // if the array is empty
  if(arr.length > 0){
    if(Array.isArray(arr[0])){
      return sumValues(arr[0]);
    } // if the next element is an array
    return arr.shift() + sumValues(arr);
  }
}

You can use Number.isFinite(value) to determine whether a variable is a number other than NaN or Infinity . 您可以使用Number.isFinite(value)来确定变量是否为NaNInfinity以外的数字。

Based on this test, check and conditionally add values to the summation. 根据此测试,检查并有条件地将值添加到求和中。

 function sumValues(arr){ if (arr.length === 0){ return 0; } // if the array is empty if (arr.length > 0) { if (Array.isArray(arr[0])){ return sumValues(arr[0]); } // if the next element is an array // pop the first element off the array var value = arr.shift(); // check its type and conditionally add it to the summation return (Number.isFinite(value) ? value : 0) + sumValues(arr); } } var arr = [1,'a','b',2,[1],'c'] console.log(sumValues(arr)); // 4 arr = [1,'3','b',2,[1,[4,3]],'c']; console.log(sumValues(arr)); // 11 (because '3' is ignored) 

You can use isNaN() to test if something is Not a Number. 您可以使用isNaN()测试某些东西是否不是数字。 By using the boolean inversion operator ! 通过使用布尔反转运算符! you can test if it's a number: !isNaN(variable) 您可以测试它是否为数字: !isNaN(variable)

Basically it says: if variable is Not Not A Number 基本上说:如果变量不是不是数字
A double negative is a positive, so it becomes: if variable Is a Number 双负数是正数,因此变为:如果变量是数字

 function sumValues(arr){ if(Array.isArray(arr)) { if(arr.length > 0) { // Get the first value and remove it from the array. var first = arr.shift(); // Test if it's numeric. if(!isNaN(first)) { // If it is, parse the value, add the rest resulting array values. return parseInt(first) + parseInt(sumValues(arr)); } // if the first item is an array, we need to iterate that one too. if(Array.isArray(first)) { return parseInt(sumValues(first)) + parseInt(sumValues(arr)); } // It isn't a number, just continue with what's left of the array. return parseInt(sumValues(arr)); } // The array is empty, return 0. return 0; } // It isn't an array else { // Is it an number? if(!isNaN(arr)) { // return the number return parseInt(arr); } // return 0, it's a dead end. return 0; } } var arr = [1,'a','b',2,[1],'c']; console.log(sumValues(arr)) // => 4 . arr = [1,'3','b',2,[1,[4,3]],'c']; console.log(sumValues(arr)) // => 14 . arr = 'foobar'; console.log(sumValues(arr)) // => 0 . arr = '10'; console.log(sumValues(arr)) // => 10 . arr = ['foobar',10,'20',[10,20,'foobar']]; console.log(sumValues(arr)) // => 60 . 

You're squashing an array, there is no need to create your own reduction function. 您要压缩数组,无需创建自己的约简函数。 Use Array#reduce to squash the array. 使用Array#reduce数组。 Upon each iteration, check if the current element is a number; 每次迭代时,检查当前元素是否为数字;否则为0。 if it is, add it to the accumulator; 如果是,则将其添加到累加器中; if not, move on. 如果没有,继续前进。

To make it recursive (as shown in the example below) check if the current element is an array; 要使其递归(如下例所示),请检查当前元素是否为数组。 if it is, add the result of calling the function on the element to the accumulator. 如果是,则将对元素调用函数的结果添加到累加器。

 const input = [1,'a','b',2,[1],'c']; const sumValues = i => i.reduce((m, e) => m + (e instanceof Array ? sumValues(e) : (isNaN(e) ? 0 : e)), 0); console.log(sumValues(input)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM