Javascript Array Amount

How can I sum the values ​​populated in the unitprice array using javascript Here is my html.

<td> <input type="text" style="width: 60px; background: none repeat scroll 0% 0% rgb(255, 255, 255);" maxlength="4" id="unitprice" name="unitprice[]"> </td> <td> <input type="text" style="width: 60px; background: none repeat scroll 0% 0% rgb(255, 255, 255);" maxlength="4" id="unitprice" name="unitprice[]"> </td> 
+6
source share
4 answers

Modify your HTML to use class instead of id ( id must be unique):

 <td> <input type="text" style="width: 60px; background: none repeat scroll 0% 0% rgb(255, 255, 255);" maxlength="4" class="unitprice" name="unitprice[]"> </td> <td> <input type="text" style="width: 60px; background: none repeat scroll 0% 0% rgb(255, 255, 255);" maxlength="4" class="unitprice" name="unitprice[]"> </td> 

Then you can summarize via JavaScript (using the jQuery .each() function):

 var totalUnitPrice = 0; $('.unitprice').each(function(index) { totalUnitPrice += parseInt($(this).val()); // parse the value to an Integer (otherwise it'll be concatenated as string) or use parseFloat for decimals }); 
+5
source

If you can get the values ​​in an array, you do not need to use jQuery to sum them up. You can use the methods already present on the array object to do the work.

Arrays have a .reduce () method. Documentation: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce

Array.reduce takes a function as an argument, which acts as a battery callback. The accumulator function takes 4 arguments (previousValue, currentValue, index, array). You only need to summarize 2 of them. These 2 arguments are previousValue and currentValue.

 var sampleArray = [1, 2, 3, 4, 5]; var sum = sampleArray.reduce(function(previousValue, currentValue){ return currentValue + previousValue; }); 

If you encounter a compatibility issue when the target environment does not support the addition of ECMAScript 5 or higher, use the prototype definition defined in the associated MDN article. (Added below)

 if (!Array.prototype.reduce) { Array.prototype.reduce = function reduce(accumulator){ if (this===null || this===undefined) throw new TypeError("Object is null or undefined"); var i = 0, l = this.length >> 0, curr; if(typeof accumulator !== "function") // ES5 : "If IsCallable(callbackfn) is false, throw a TypeError exception." throw new TypeError("First argument is not callable"); if(arguments.length < 2) { if (l === 0) throw new TypeError("Array length is 0 and no second argument"); curr = this[0]; i = 1; // start accumulating at the second element } else curr = arguments[1]; while (i < l) { if(i in this) curr = accumulator.call(undefined, curr, this[i], i, this); ++i; } return curr; }; } 
+35
source
 function getSum(){ var ups = document.getElementsByName('unitprice[]'), sum = 0, i; for(i = ups.length; i--;) if(ups[i].value) sum += parseInt(ups[i].value, 10); return sum; } 
+2
source

Give unique <input> identifiers as follows:

 <input type="text" id="unitprice_1"> <input type="text" id="unitprice_2"> <input type="text" id="unitprice_3"> 

Then we calculate the amount:

 var i = 1; var sum = 0; var input; while( ( input = document.getElementById( 'unitprice_'+i ) ) !== null ) { sum += parseInt( input.value ); ++i; } alert( sum ); 
0
source

Source: https://habr.com/ru/post/895963/


All Articles