Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a bit of a logic problem with my code here, and was wondering if anybody could point me in the right direction.

Constraints:

  1. Each item pushed will be an event, grouped by "name" and displayed inline in a chart.
  2. EPC, M1, or M2 may have the value of null.
  3. An event should be created with a 'start' of first non-null timeline and 'end' of the next non-null timeline value. (no need to check beyond m2 since none will contain null value). There should be at least 4 milestones in every group.
  4. Should continue until no more 'groups' exist (content[i].name).

Currently, my code has a couple of issues:

  1. Only creates one event per group.
  2. EPC not accounted for in iteration, and unhandled if start and non-null.

Here is the code:

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
 });
}

And here is the array content being read:

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
}
}];

Currently, all events created also begin with m3. Thanks for any help!

share|improve this question
    
Please indent your code properly. Please use local variables, declared with var. Please don't use unintroduced variables like data. –  Bergi Dec 18 '12 at 22:26
    
Your second return statement in getMilestone seems wrong, and please get rid of the annoying repetitions in getSecondStone. –  Bergi Dec 18 '12 at 22:27
    
possible duplicate of previous question Converting JSON data array to different structure with conditionals –  Bergi Dec 19 '12 at 0:16

1 Answer 1

I left a comment on your previous question. Here is the implementation of my comment.

// 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
        });
    }

}
share|improve this answer
    
Thanks this works great! –  user1898946 Dec 20 '12 at 6:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.