0

我的代码在这里有一点逻辑问题,想知道是否有人能指出我正确的方向。

约束:

  1. 推送的每个项目都是一个事件,按“名称”分组并内联显示在图表中。
  2. EPC、M1 或 M2 的值可能为 null。
  3. 应该使用第一个非空时间线的“开始”和下一个非空时间线值的“结束”来创建事件。(无需检查 m2 以外的内容,因为没有任何内容包含空值)。每组至少应有 4 个里程碑。
  4. 应该继续,直到不再存在“组”(内容[i].name)。

目前,我的代码有几个问题:

  1. 每组只创建一个事件。
  2. EPC 在迭代中未考虑,并且如果开始且非空则未处理。

这是代码:

getMileStone = function(obj) {
    if(!obj) { return };
        propCounter = 1;
        newcount = 0;
        for(propCounter = 1; propCounter <= 7; propCounter++) {
            if(obj.timeline["m" + propCounter]) {
                data[newcount] = {key: "m" + propCounter, value: obj.timeline["m" + propCounter]};
                return data[newcount];
                newcount++;
            }
         }

        };

        getSecondStone = function(obj) {

            switch(getMileStone(current).key){

                case 'm1': return {value: obj.timeline['m2']};
                break;
                case 'm2': return {value: obj.timeline['m3']};
                break;
                case 'm3': return {value: obj.timeline['m4']};
                break;
                case 'm4': return {value: obj.timeline['m5']};
                break;
                case 'm5': return {value: obj.timeline['m6']};
                break;
                case 'm6': return {value: obj.timeline['m7']};
                break;
                default: return {value: obj.timeline['m1']};
            }

        };

for(i=0;i< content.length;i++) {
 current = content[i];
 firstMileStone = getMileStone(current); 
 secondMileStone = getSecondStone(current);
  result.push({
  'start': new Date(current.epc || firstMileStone.value),
  'end': new Date(current.m1 || secondMileStone.value),
  'content': firstMileStone.key,
  'group' : current.name,
  'className' : firstMileStone.key
 });
}

这是正在读取的数组内容:

content = [{
"name": "5-HP-N/A-N/A-F8",
"node": {
    "name": "5",
    "id": 14
},
"timeline": {
    "epc": null,
    "m1": null,
    "m2": null,
    "m3": 1554087600000,
    "m4": 1593572400000,
    "m5": 1625108400000,
    "m6": 1641006000000,
    "m7": 1656644400000
},
"fab": {
    "name": "F8",
    "id": 1
  }
},
{
  "name": "7-HP-N/A-N/A-F8",
  "node": {
    "name": "7",
    "id": 15
},
"timeline": {
    "epc": null,
    "m1": null,
    "m2": null,
    "m3": 1491015600000,
    "m4": 1530414000000,
    "m5": 1561950000000,
    "m6": 1577847600000,
    "m7": 1593572400000
},
"fab": {
    "name": "F8",
    "id": 1
  }
},
{
"name": "5-XM-N/A-PLT-F8",
"node": {
    "name": "5",
    "id": 14
},
"timeline": {
    "epc": null,
    "m1": null,
    "m2": null,
    "m3": 1554087600000,
    "m4": 1593572400000,
    "m5": 1625108400000,
    "m6": 1641006000000,
    "m7": 1656644400000
},
"fab": {
    "name": "F8",
    "id": 1
  }
},
{
"name": "40-LP-TFS-FSL-F7",
"node": {
    "name": "40",
    "id": 4
},
"timeline": {
    "epc": 1349060400000,
    "m1": null,
    "m2": null,
    "m3": 1262314800000,
    "m4": 1301626800000,
    "m5": 1333249200000,
    "m6": 1341111600000,
    "m7": 1357009200000
},
"fab": {
    "name": "F7",
    "id": 3
  }
},
{
"name": "40-LP-SST-TI-F7",
"node": {
    "name": "40",
    "id": 4
},
"timeline": {
    "epc": 1349060400000,
    "m1": null,
    "m2": null,
    "m3": 1262314800000,
    "m4": 1301626800000,
    "m5": 1333249200000,
    "m6": 1341111600000,
    "m7": 1357009200000
},
"fab": {
    "name": "F7",
    "id": 3
  }
},
{
"name": "28-LPQ-TN3-QCOM-F1",
"node": {
    "name": "28",
    "id": 2
},
"timeline": {
    "epc": 1349060400000,
    "m1": null,
    "m2": null,
    "m3": 1285902000000,
    "m4": 1325386800000,
    "m5": 1357009200000,
    "m6": 1372647600000,
    "m7": 1388545200000
},
"fab": {
    "name": "F1",
    "id": 2
}
}];

目前,所有创建的事件也以 m3 开头。谢谢你的帮助!

4

1 回答 1

0

我对你之前的问题发表了评论。这是我的评论的实现。

// get all non-null milestones
getMileStone = function (obj) {
    var result = [];

    for (var o in obj.timeline) {
        if (obj.timeline[o]) {
            result.push({ key: o, value: obj.timeline[o] });
        }
    }
    //dummy value for end pairing
    result.push({key: 'end', value : null});
    return result;
};


// loop over content array (seems like you have an array of objects)
for (var i = 0; i < content.length; i++) {
    current = content[i];
    var milestones = getMileStone(current);

    //loop through the non-null milestones
    for (var j = 0; j < milestones.length - 1; j++) {
        result.push({
            'group': current.name,
            'content': milestones[j].key,
            'start': new Date(milestones[j].value),
            'end': milestones[j + 1].value != null ? new Date(milestones[j + 1].value):null,
            'classname': milestones[j].value
        });
    }

}
于 2012-12-19T00:12:26.127 回答