jQuery数组乘法&加成

时间:2017-03-23 12:37:37

标签: jquery arrays multiplication

我有两个数组:

例如:count["1","3","1"..........]amount["10","20","30"..........]

我需要执行乘法和放大另外:

var total = (1x10)+(2x20)+(1x30)...........

任何人都可以提供帮助。感谢

2 个答案:

答案 0 :(得分:1)

您可以使用此代码



var count = [1, 3, 1];
var amount = [10, 20, 30];
var sum = 0;
for (var i = 0; i < count.length; i++) {
  sum += count[i] * amount[i];
}
console.log(sum)
&#13;
&#13;
&#13;

答案 1 :(得分:0)

var a = ["1","3","1"], b = ["10","20","30"], sum = 0;
$.each(a, function(k,v){
	sum += (parseInt(v) * parseInt(b[k]));
})
console.log(sum); // 100
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

100