简体   繁体   English

javascript总结数组中的值而不是数组中的所有值

[英]javascript sum up value in array and not all values in array

Right now I am working on this function to litterlay sum up. 现在我正在研究这个功能,以便总结一下。 The array looks for example like this: var array = ['5','2','8']; 该数组的示例如下: var array = ['5','2','8']; I want to litterly say array[0] +1 or something like this. 我想乱扔一下array[0] +1或类似的东西。 I look at reduce but that sums up all the values with each other, that is not what it is supposed to do. 我看一下reduce,但是它总结了所有的值,这不是它应该做的。 What should come out is when sum is 1, and you want the first value in the array. 应该得出的是当sum为1时,你想要数组中的第一个值。 In this case is the first one is 5 : 5 + 1 = 6 . 在这种情况下,第一个是5: 5 + 1 = 6 Then 6 should come out of it and replace the place of 5 in the array. 然后6应该从它出来并替换阵列中5的位置。

This is an example of what I got right now. 这是我现在所得到的一个例子。

 var array = ['6','2','5']; for(var i = 0; i < array.length; i++){ array = array[i]; var sum = 1; array = sum + array[0]; console.log(array); } 

I want to litterly say array[0] +1 or something like this. 我想乱扔一下[0] +1或类似的东西。

Looks like you want to increment all the values by 1 . 看起来你想要将所有值增加1

Try this appoach 尝试这个appoach

 var array = ['6','2','5']; array = array.map( a => String( Number(a) + 1 ) ); console.log(array); 

If you want to keep them Number instead of String , then remove the wrapping with String constructor 如果要将它们保留为Number而不是String ,则使用String构造函数删除包装

 var array = ['6','2','5']; array = array.map( a => Number(a) + 1 ); console.log(array); 

Edit - Based on comment and question update 编辑 - 基于评论和问题更新

no I want to add 1 to only one value in the array and not all of them 不,我想在数组中只添加一个值,而不是全部

What should come out is when sum is 1, and you want the first value in the array. 应该得出的是当sum为1时,你想要数组中的第一个值。

No need for iteration then 那么就不需要迭代了

 var array = ['6','2','5']; var sum = 1; array[ sum - 1 ] = String( Number( array[ sum - 1 ] ) + 1 ); console.log(array); 

You can use unary plus operator to convert the first element array[0] to a number and than add 1 and finally add '' to make it a string again: 您可以使用一元加运算符将第一个元素array[0]转换为数字而不是添加1,最后添加''以使其再次成为字符串:

 var array = ['6','2','5']; array[0] = +array[0] + 1 + ''; console.log(array); 

For a single value, you could take the value (taking an unary plus + for making a number) and increment it (make a string again), then assign it to the same index. 对于单个值,您可以获取值(使用一元加上+来表示数字)并递增它(再次生成一个字符串),然后将其分配给相同的索引。

 var array = ['6', '2', '5']; array[0] = (+array[0] + 1).toString(); console.log(array); 

Array.prototype.addScalar=function(value){
   var cloneArray=[];
   this.forEach(function(entry){
     //it is weird that your array is ['1', '2'] instead of [1,2]
     //but this "if" handles it
     if ( typeof(entry)==='string') {
        cloneArray.push(parseInt(entry)+value);
     } else {
        cloneArray.push(entry+value);
     }
   });
   return cloneArray;
}

['1',2,3].addScalar(1) //[2, 3, 4]

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

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