简体   繁体   English

从起始位置求和下一个数组元素(javascript)

[英]Sum next array elements from a starting position (javascript)

I need this for dynamically use. 我需要这个来动态使用。 I have 2 associated arrays: 我有2个关联的数组:

Var $some = [1, 2, 3];
Var $other = [a, b, c];

a, b, c are some html ids and 1, 2, 3 are their values. a,b,c是一些html id,而1,2,3是它们的值。 I want something like this on trigger: 我想要类似这样的触发器:

$'#a').attr('max', //b+c val(2+3)
$'#b').attr('max', //a+c val(1+3)
$'#c').attr('max', //a+b val(1+2)

I ve searched here get prev and next items in array about finding next elements in array, and I think would be usefull a loop that itinerate elements a (then b then c) with its values and for each one execute a code like: sum next elements to them. 我在这里搜索了关于在数组中查找下一个元素的数组中的上一个和下一个项目 ,我认为用一个循环遍历元素a(然后是b然后是c)的元素的值并为每个元素执行一个代码,例如:sum next元素。 Substracting element value after sumed won't work pripperly because I want that script on some ranges change... So...my question would be: how can I sum elements itinerated from a loop? 求和后减去元素值将无法正常工作,因为我希望该脚本在某些范围内发生变化...所以...我的问题是:如何求和从循环中迭代的元素? I think that would be the key to solve this because I can loop it 2 times in this example and would stop without started element... 我认为这将是解决此问题的关键,因为在此示例中我可以将其循环2次,并且在没有开始元素的情况下会停止...

If your some and other are equal, so you can try this solution. 如果您的someother人相等,则可以尝试此解决方案。

 const some = [1, 2, 3]; const other = ['a', 'b', 'c']; other.forEach((item, index) => { const sum = some.reduce((s, c, i) => index !== i ? s += c : s, 0); $(`#${item}`).text(sum); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p id="a"></p> <p id="b"></p> <p id="c"></p> 

I am iterating over each item in the other array. 我正在遍历other数组中的每个项目。 The functions passed into the forEach gets two parameters - the current item and it's index in the other array. 传递给forEach的函数有两个参数-当前项目及其在other数组中的索引。 Then I call reduce on the some array and pass a function into it which gets three parameters - the accumulate value , the current item and it's index in the some array. 然后,我在some数组上调用reduce并将其传递给函数,该函数获得三个参数- accumulate value ,当前项及其在some数组中的索引。 In the body I check if the some current item index is not equal to the other current item index ( this prevent for a to add the value for a ) so I add it to the s , which contains the result in each iteration of some.reduce a return it which is done automatically in one line statement if not return the result without adding the current item value. 在主体我检查,如果some当前项索引不等于所述other当前项的索引(这防止了a用于添加值a ),所以我将其添加到s ,其中包含的结果的每次迭代some.reduce如果没有返回结果而不添加当前项目值,则some.reduce一条返回语句,该返回结果将在一行语句中自动完成。

You can write the reduce function like this to be more readable 您可以像这样编写reduce函数,使其更具可读性

some.reduce((s, c, i) =>  {
   if(index !== i) {
      s += c; 
   }

   return s;
}, 0)

Assuming that key of other is the same with it's corresponding value on some 假设该键other ,就是与它相同的上相应的值some

 const some = [1, 2, 3]; const other = [a, b, c]; $.each(other, function(key, value){ // Loop thru each id var sum = some.reduce((x, y) => x + y) - some[key]; // add each 'some' elements and subtracting the id's value $(value).attr('max', sum); // set max attr of id equal to sum }); // log output on console console.log(a); console.log(b); console.log(c); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" id="a"> <input type="number" id="b"> <input type="number" id="c"> 

Using html(function) to do the loop indexing and a reduce() for sum 使用html(function)进行循环索引并使用reduce()求和

 var some = [1, 2, 3]; var other = ['a', 'b', 'c']; $('#'+ other.join(',#')).html((elIdx)=>{ return some.reduce((a,c,i) => a+= (i===elIdx ? 0 : c), 0); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p id="a"></p> <p id="b"></p> <p id="c"></p> 

Previous solutions works nice, of course, but, just in case, here is more straightforward solution: 当然,以前的解决方案效果很好,但是以防万一,这里是更简单的解决方案:

$( "input" ).each(function( index ) {
sum=0;
for(i=0;i<$(this).siblings().length;i++) {
sum+= parseInt($( this ).siblings().eq(i).val());
}
console.log(sum);
$(this).attr('max',sum)
});

 $( "input" ).each(function( index ) { // loop through each input sum=0; for(i=0;i<$(this).siblings().length;i++) { // get all siblings sum+= parseInt($( this ).siblings().eq(i).val()); //calculate sum of siblings values } console.log(sum); $(this).attr('max',sum) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="inputs"> <input type="number" id="a" value=1> <input type="number" id="b" value=2> <input type="number" id="c" value=3> </div> 

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

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