Preface
I encountered a problem in my work recently. The requirement is to use Javascript to calculate the duplicate values of a two-dimensional array, such as a two-dimensional array below.
[[\'error\',3],[\'error\',5],[\'error\',6],[\'true\',3],[\'true\',1]]
The duplicates \'error\' and \'true\' need to be counted statistically,
Results after statistical calculation:
[[\'error\',14],[\'true\',4]]
Implementation code:
var arr = [[\'error\',3],[\'error\',5],[\'error\',6],[\'true\',3],[\'true\',1]]; var obj = {}; var result = []; (function(arr){ obj[arr[0]] = obj[arr[0]]? obj[arr[0]] + arr[1] : arr[1]; }); for (var i in obj){ ([i,obj[i]]) }
Summarize
The above is the entire content of this article. I hope it can help you study or work. If you have any questions, you can leave a message to communicate.