循环中的json对象修改不起作用

json object modification within loop is not working

本文关键字:修改 不起作用 对象 json 循环      更新时间:2023-09-26

我从web服务返回了json对象。Web服务对象:

var seriesData = [{
    feederId: "PTS113T",
    businessEventCode: "LowVoltage",
    servicePointEventCount: 6,
    startDayID: 20130812,
}, {
    feederId: "PTS113T",
    businessEventCode: "HighVoltage",
    servicePointEventCount: 2,
    startDayID: 20130812,
}, {
    feederId: "PTS113T",
    businessEventCode: "LowVoltage",
    servicePointEventCount: 7,
    startDayID: 20130814,
}, {
    feederId: "PTS113T",
    businessEventCode: "HighVoltage",
    servicePointEventCount: 5,
    startDayID: 20130815,
}];

我想把那个对象修改成。。

我有startDayID,对于每个startDayIDbusinessEventCode: "LowVoltage"businessEventCode: "HighVoltage", 将有两个值或一个值

想要:

var seriesData = [{
    feederId: "PTS113T",
    servicePointEventLowCount: 6,
    servicePointEventHighCount: 2,
    startDayID: 20130812,
}, {
    feederId: "PTS113T",
    servicePointEventLowCount: 7,
    startDayID: 20130814,
}, {
    feederId: "PTS113T",
    servicePointEventHighCount: 5,
    startDayID: 20130815,
}];

我在做什么:

if (seriesData) {
    var mockdata = [];
    for (var i = 0; i < seriesData.length; i += 2) {
        var data = {};
        //data.feederId = seriesData[i].feederId;
        if ((seriesData[i].startDayID) == (seriesData[i + 1].startDayID)) {
            data.feederId = seriesData[i].feederId;
            data.servicePointEventLowCount = seriesData[i].servicePointEventCount;
            data.servicePointEventHighCount = seriesData[i + 1].servicePointEventCount;
            data.startDayID = seriesData[i].startDayID;
        } else {
            data.feederId = seriesData[i].feederId;
            data.startDayID = seriesData[i].startDayID;
            if (seriesData[i].businessEventCode == 'LowVoltage') {
                data.servicePointEventLowCount = seriesData[i].servicePointEventCount;
                data.servicePointEventHighCount = 0;
            } else {
                data.servicePointEventHighCount = seriesData[i].servicePointEventCount;
                data.servicePointEventLowCount = 0;
            }
       }
        mockdata.push(data);
    }

试试这个。

var mockdata = [];
// for check whether startDayID exist
//and temporary storage for data relative key(startDayID) 
var mockDataKeyMap ={}; 
for (var i = 0; i < seriesData.length; i ++) {
    var item = seriesData[i];
    var key = item.startDayID;
    var data = null;
    if(!mockDataKeyMap[key]){
        // startDayID do not exist create new
        data = {};
        // save for get after (below else sentence)
        mockDataKeyMap[key] = data; 
        // push into
        mockdata.push(data);
        // set default info
        data.feederId = item.feederId;
        data.startDayID = item.startDayID;
    }else{
        // if startDayID exist get data from map
        data= mockDataKeyMap[key]; 
    }
    if (item.businessEventCode === 'LowVoltage') {
        data.servicePointEventLowCount = item.servicePointEventCount;
    } 
    if (item.businessEventCode === 'HighVoltage') {
        data.servicePointEventHighCount = item.servicePointEventCount;
    }
}

使用"i+=2" 是非常危险的

您的seriesData总是没有两个数据LowVoltageHighVoltage

然后检查所有数据i++

然后只检查密钥是否已经存在

try:

for (var i = 0; i < seriesData.length-1; i += 2) {

根据上一次运行的循环,您将不会有seriesData[i + 1].startDayID在处理时更好地检查此条件。