How I can sum up the values filled in 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>
sum() function in D3. js is used to return the sum of the given array's elements. If the array is empty then it returns 0. Parameters: This function accepts a parameters Array which is an array of elements whose sum are to be calculated.
Approach to Find the Sum of All Elements in an ArrayInitialize a variable sum to store the total sum of all elements of the array. Traverse the array and add each element of the array with the sum variable. Finally, return the sum variable.
For a given array, there can be many sum queries.
If you can get the values in an array, you don't have to use jQuery to sum them. You can use 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 accepts a function as an argument that acts as an accumulator callback. The accumulator function accepts 4 arguments (previousValue, currentValue, index, array). You only need 2 of them to sum. Those 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 are faced with a compatibility issue where the target environment doesn't support ECMAScript 5 or above additions, use the prototype definition defined in the MDN article linked. (Appended 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;
};
}
Change 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 total via JavaScript (using 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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With