Objective
A problem has recently been encountered with the need to compute duplicate values of two-dimensional arrays using JavaScript, as there is a two-dimensional array below
[[\ ' error\ ', 3],[\ ' error\ ', 5],[\ ' error\ ', 6],[\ ' true\ ', 3],[\ ' true\ ', 1]]
Statistics are needed to compute duplicates \ ' error\ ' and \ ' true\ ',
Results after the statistical calculation:
[[\ ' error\ ', 14],[\ ' true\ ', 4]]
Implementation code:
var arr = [[\ ' error\ ', 3],[\ ' error\ ', 5],[\ ' error\ ', 6],[\ ' true\ ', 3],[\ ' true\ ', 1]];
var obj = {};
var result = [];
Arr.foreach (function (arr) {
obj[arr[0]] = obj[arr[0]]? Obj[arr[0]] + arr[1]: arr[1];
for (var i in obj) {
Result.push ([i,obj[i]])
}
Summarize
The above is the entire content of this article, I hope to be able to learn or work can bring some help, if you have questions you can message exchange.