简体   繁体   中英

Sum next array elements from a starting position (javascript)

I need this for dynamically use. I have 2 associated arrays:

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

a, b, c are some html ids and 1, 2, 3 are their values. 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. 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...

If your some and other are equal, so you can try this solution.

 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. The functions passed into the forEach gets two parameters - the current item and it's index in the other array. 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. 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.

You can write the reduce function like this to be more readable

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

 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

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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