SOURCE

var biaoyangStr ="[{\"id\":1,\"company\":\"两湖街道办\",\"duChaCompany\":\"县委县政府督查局\"},"+
"{\"id\":2,\"company\":\"两湖街道办\",\"duChaCompany\":\"县委县政府督查局\"},"+
"{\"id\":3,\"company\":\"两湖街道办\",\"duChaCompany\":\"县委县政府督查局\"},"+
"{\"id\":4,\"company\":\"两湖街道办\",\"duChaCompany\":\"县委县政府督查局\"},"+
"{\"id\":5,\"company\":\"两湖街道办\",\"duChaCompany\":\"县委县政府督查局\"},"+

"{\"id\":126,\"company\":\"总工会\",\"duChaCompany\":\"县委县政府督查局\"}]"



var pipingStr = 
"[{\"id\":1,\"company\":\"洧川镇\",\"duChaCompany\":\"县委县政府督查局\"},"+
"{\"id\":2,\"company\":\"朱曲镇\",\"duChaCompany\":\"县委县政府督查局\"},"+
"{\"id\":3,\"company\":\"蔡庄镇\",\"duChaCompany\":\"县委县政府督查局\"},"+

"{\"id\":46,\"company\":\"住建局\",\"duChaCompany\":\"县委县政府督查局\"}]"


ResultData = function (id, company, cityAdmirTimes, areaAdmirTimes, admirMoney,
    cityPipingTimes, areaPipingTimes, pipingMoney) {
    this.id = id;
    this.company = company;
    this.cityAdmirTimes = cityAdmirTimes;
    this.areaAdmirTimes = areaAdmirTimes;
    this.admirMoney = admirMoney;
    this.cityPipingTimes = cityPipingTimes;
    this.areaPipingTimes = cityPipingTimes;
    this.pipingMoney = pipingMoney;
}

var resultDatas = new Array;


//这里开始是处理表彰的
var admirDatas = JSON.parse(biaoyangStr);
var cityTimes = 0, areaTimes = 0, itemId = 1, cityMultiple = 1.0, areaMultiple = 0.5;
for (var i = 0; i < admirDatas.length; i++) {
    //这里是确定到底是市局还是县局
    if (admirDatas[i].duChaCompany == "县委县政府督查局") {
        areaTimes++;
    } else {
        cityTimes++;
    }

    var resultdata = {
        id: itemId,
        company: admirDatas[i].company,
        cityAdmirTimes: cityTimes,
        areaAdmirTimes: areaTimes,
        admirMoney: 0,
        cityPipingTimes: 0,
        areaPipingTimes: 0,
        pipingMoney: 0
    };



    //将第一个item添加到结果数组中
    if (i == 0) {
        resultDatas.push(resultdata);
    } else if (i > 0) {
        if (admirDatas[i].company == admirDatas[i - 1].company) {
            resultDatas[resultDatas.length - 1] = resultdata;
        } else {

            //将奖励次数置空
            if (admirDatas[i].duChaCompany == "县委县政府督查局") {
                areaTimes = 1;
                cityTimes = 0;
            } else {
                areaTimes = 0;
                cityTimes = 1;
            }
            resultdata.cityAdmirTimes = cityTimes;
            resultdata.areaAdmirTimes = areaTimes;

            //这里是将新的item加上去
            itemId++;
            resultdata.id = itemId;
            resultDatas.push(resultdata);


            //这里计算前一个奖惩了多少钱
            var cityTime = resultDatas[resultDatas.length - 2].cityAdmirTimes;
            var areaTime = resultDatas[resultDatas.length - 2].areaAdmirTimes;

            var admirMoney = cityTime * cityMultiple + areaMultiple * areaTime;
            resultDatas[resultDatas.length - 2].admirMoney = admirMoney;
        }
    }
}

//这里计算最后一个奖惩了多少钱
var cityTime = resultDatas[resultDatas.length - 1].cityAdmirTimes;
var areaTime = resultDatas[resultDatas.length - 1].areaAdmirTimes;

var admirMoney = cityTime * cityMultiple + areaMultiple * areaTime;
resultDatas[resultDatas.length - 1].admirMoney = admirMoney;





///////////////////////////我是华丽的分割线/////////////////////////////////
var criticismDatas = JSON.parse(pipingStr);

for (var i = 0; i < resultDatas.length; i++) {
    var itemData = resultDatas[i];
    var cityTimes = 0, areaTimes = 0;
    var currentName = "";
    var haveItem = false;
    for (var j = 0; j < criticismDatas.length; j++) {
        var companyName = criticismDatas[j].company;
        if (criticismDatas[j].company == resultDatas[i].company) {
            haveItem = true;
            if (criticismDatas[j].duChaCompany == "县委县政府督查局") {
                areaTimes++;
            } else {
                cityTimes++;
            }
        }

        if (j == criticismDatas.length - 1) {
            if (haveItem) {
                resultDatas[i].cityPipingTimes = cityTimes;
                resultDatas[i].areaPipingTimes = areaTimes;
                resultDatas[i].pipingMoney = cityTimes * -1.0 + areaTimes * -0.5;
                haveItem = false;
            } else {

                // itemId++;
                // var resultdata = {
                //     id: itemId,
                //     company: companyName,
                //     cityAdmirTimes: 0,
                //     areaAdmirTimes: 0,
                //     admirMoney: 0,
                //     cityPipingTimes: cityTimes,
                //     areaPipingTimes: areaTimes,
                //     pipingMoney: 0
                // };
                // resultDatas.push(resultdata);
            }

            cityTimes = 0, areaTimes = 0;
        }
    }
}

//定义一个pipingDatas,让它获取到resultdata里面没有的项
var pipingDatas = new Array;
for (var i = 0; i < criticismDatas.length; i++) {
    for (var j = 0; j < resultDatas.length; j++) {
        var isExist = false;
        if (criticismDatas[i].company == resultDatas[j].company) {
            isExist = true;
            break;
        }

        if (j == resultDatas.length - 1 && !isExist) {
            pipingDatas.push(criticismDatas[i]);

        }
    }
}

for (var i = 0; i < pipingDatas.length; i++) {
    for (var j = i; j < resultDatas.length; j++) {
        if (pipingDatas[i].company == resultDatas[j].company) {
            if (pipingDatas[i].duChaCompany == "县委县政府督查局") {
                resultDatas[j].areaPipingTimes = resultDatas[j].areaPipingTimes + 1;
            } else {
                resultDatas[j].cityPipingTimes = resultDatas[j].cityPipingTimes + 1;
            }

            break;
        }

        if (j == resultDatas.length - 1) {
            var cityTimes = 0, areaTimes = 0;
            if (pipingDatas[i].duChaCompany == "县委县政府督查局") {
                areaTimes++;
            } else {
                cityTimes++;
            }


            itemId++;
            //这里我也不知道为什么,反正就是要剪一个数
            if(cityTimes>0){
                cityTimes -- 
            }
            if(areaTimes >0){
                areaTimes --
            }

            var resultdata = {
                id: itemId,
                company: pipingDatas[i].company,
                cityAdmirTimes: 0,
                areaAdmirTimes: 0,
                admirMoney: 0,
                cityPipingTimes: cityTimes,
                areaPipingTimes: areaTimes,
                pipingMoney: 0
            };
            resultDatas.push(resultdata);
            areaTimes = 0, citytimes = 0;
        }
    }
}


// for (var i = 0; i < resultDatas.length; i++) {
//     document.write(
//         resultDatas[i].id + " " + resultDatas[i].company + " " +
//         resultDatas[i].cityAdmirTimes + " " + resultDatas[i].areaAdmirTimes + " " +
//         resultDatas[i].admirMoney + " " +
//         resultDatas[i].cityPipingTimes + " " + resultDatas[i].areaPipingTimes + " " +
//         resultDatas[i].pipingMoney + "</br>");
// }

 document.write(
      JSON.stringify( resultDatas ))



console 命令行工具 X clear

                    
>
console