Sum values from an Array in JavaScript

前端 未结 10 1112
离开以前
离开以前 2020-12-02 18:17

I have defined a JavaScript variables called myData which is a new Array like this:

var myData = new Array([\'2013-01-22\', 0], [\'         


        
10条回答
  •  盖世英雄少女心
    2020-12-02 18:57

    Try the following

    var myData = [['2013-01-22', 0], ['2013-01-29', 1], ['2013-02-05', 21]];
    
    var myTotal = 0;  // Variable to hold your total
    
    for(var i = 0, len = myData.length; i < len; i++) {
        myTotal += myData[i][1];  // Iterate over your first array and then grab the second element add the values up
    }
    
    document.write(myTotal); // 22 in this instance

提交回复
热议问题