0

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...

4 Answers 4

1

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.reducea 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)
Sign up to request clarification or add additional context in comments.

11 Comments

I will have a look to understand your code...foreach will run for every item, and index is what for?
@Florin also you need to add it in the max attibute, I put it in the html to show the result
Something interesing here... In the documentation gave me an idea on the point with initial value. Inserting initial value gets the value from where to start suming...is there any function that makes what reduce make but substracting values(gave in array) from an initial value ? Like in my initial question "-" instead of +? Sorry if is dumb quest
Just pass what value you want to be initial and use - sign
Yeah, I realised that later... If I add more html elements, in order to make it works, I can add more letters in parameters and along the functions?(sorry, I trying to understand how really parameters works)
|
1

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">

Comments

0

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>

Comments

0

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>

1 Comment

I will consider your solutions too, because what I ve asked and what all of you answered me is not the solve of my plan, is just a way to start from. Ty! I find your solution more dynamically for what I am propose to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.