如何使用JavaScript总结独特的价值 [英] How to sum up distinct value using javascript
问题描述
我想尝试从列表中总结出明显的价值..如果只有2条相似的记录,我现在能够做到.如果超过2,我将无法进行检查.以下是JavaScript代码:
I want to try and sum up distinct value from a list.. currently i am able to do so if theres only 2 similar record. If theres more than 2 i am not able to do the checking. Following is the javascript code:
function validateData(){
var total = document.frm.size.value;
var msg="";
var tbxA;
var tbxB;
var tbxA2;
var tbxB2;
var tbxC;
var totalValue =0;
var repeatedValue= 0;
var row = 0;
var row2 = 0;
for(var i=0; i<parseInt(total); i++){
tbxA = document.getElementById('tbx_A'+i).value;
tbxB = document.getElementById('tbx_B'+i).value-0;
tbxC = document.getElementById('tbx_C'+i).value;
for(var j=i+1; j<parseInt(total); j++){
tbxA2 = document.getElementById('tbx_A'+j).value;
tbxB2 = document.getElementById('tbx_B'+j).value-0;
if (tbxA==tbxA2) {
totalValue = tbxB + tbxB2;
}
if (totalValue != tbxC) {
repeatedValue= 1;
row = i;
row2 = j;
msg+="*total value does not add up at row " +(row2+1);
break;
}
}
if(repeatedValue== 1){
break;
}
}
return msg;
}
例如A:水果的类型,B:每种水果的总和,C:一次购买的数量
C的总数应等于B.即Apple:3 + 3 + 4 =10.因此,如果总数不等于10,则会提示我一个错误.
For example A:type of fruit, B: total of each fruit, C: how many bought at a time
total of C should be equal to B. i.e Apple: 3+3+4 = 10. So if the total is not equals to 10 it should prompt me an error.
A B C
Apple 10 3
Orange 10 10
Apple - 3
Apple - 4
我上面的代码将提示错误bt,它不会超出Apple的第二次出现.
所以,是的,我应该如何确保它遍历整个列表以总结所有相似的值?
My code above will prompt error bt it doesnt go beyond 2nd occurence of Apple.
So yes, how should i go about to ensure it loop through the whole list to sum up all similar values?
在此先感谢您提供任何帮助!
Thanks in advance for any possible help!
推荐答案
尝试一下:
var total = +document.frm.size.value,
data = {};
for(var i=0; i<total; ++i) {
var key = document.getElementById('tbx_A'+i).value;
data[key] = data[key] || {B:0, C:0};
data[key].B += +document.getElementById('tbx_B'+i).value || 0;
data[key].C += +document.getElementById('tbx_C'+i).value || 0;
}
for(var i in data) {
if(data.hasOwnProperty(i) && data[i].B != data[i].C) {
return "total value does not add up";
}
}
return "";
一些评论:
-
parseInt
(和parseFloat
)非常慢.字符串之前的+
运算符将其更快地转换为数字.但是,如果您真的想确保数字是整数,请使用Math.floor()
,Math.round()
,Math.ceil()
或更快但难以辨认的|0
. - 如果您确实想要
parseInt
(例如,要将'123foobar'
转换为123
),请始终使用基数.例如:parseInt('123', 10)
- 避免在循环条件下进行计算,因为它们在每次迭代时运行.只需在循环之前进行一次计算,然后将结果保存在变量中即可.
parseInt
(andparseFloat
) is very slow.+
operator before string converts it to a number much faster. But if you really want to make sure the numbers are integers, useMath.floor()
,Math.round()
,Math.ceil()
or the faster but illegible|0
.- In case you really want
parseInt
(e.g. you want to convert'123foobar'
into123
), always use a radix. For example:parseInt('123', 10)
- Avoid doing calculations at the condition of a loop, because they run at each iteration. Just do the calculation once before the loop and save the result in a variable.
这篇关于如何使用JavaScript总结独特的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!