shenqi
2014-03-04 23:34:31 +08:00
var obj1 = [{
userid: 'james',
status: 'open',
open_count: 2
}, {
userid: 'james',
status: 'pending',
pending_count: 3
}, {
userid: 'livid',
status: 'open',
close_count: 5
}]
function objConut(obj) {
var returnObj = [], tempObj = {}, i, j, l, objI;
for( i = 0, l = obj.length; i < l; i++){
objI = obj[i];
tempObj[objI.userid] = tempObj[objI.userid] || {}
tempObj[objI.userid].userid = objI.userid;
tempObj[objI.userid][objI.status+"_count"] = objI[objI.status+"_count"] +
( tempObj[objI.userid][objI.status+"_count"] || 0 );
}
for( j in tempObj ){
returnObj.push( tempObj[j] )
}
return returnObj;
}
objConut(obj1);
try it。