数组中相同的值相加
var arr = [1,2,3,1,2,33,11,2,3,1,34,2,11]; var obj = {}; var result = []; for(var i=0;i<arr.length;i++){ if (obj[arr[i]]!=undefined) { obj[arr[i]]=obj[arr[i]]+arr[i]; }else{ obj[arr[i]]=arr[i]; } }
问卷统计里面本单位与其他单位的统计参与人数与比率
for(var i=0;i<arr2.length;i++){ if (obj2[arr2[i].split('/')[0]]!=undefined) { obj2[arr2[i].split('/')[0]]=obj2[arr2[i].split('/')[0]]+parseInt(arr2[i].split('/')[1]); }else{ obj2[arr2[i].split('/')[0]]=parseInt(arr2[i].split('/')[1]); } }
若不想相同的值相加,而是把数组去重
var arr = [1,2,3,1,2,33,11,2,3,1,34,2,11];
var obj = {};
var result = [];
for (var i = 0; i < arr.length; i++) { obj[arr[i]]=arr[i]; }