Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>see a working fiddle here: <a href="http://jsfiddle.net/K37Fa/" rel="nofollow">http://jsfiddle.net/K37Fa/</a></p> <p>your input-data seems to be an array, so i build a loop around that. if not just see this fiddle where the input data is a simple object: <a href="http://jsfiddle.net/K37Fa/1/" rel="nofollow">http://jsfiddle.net/K37Fa/1/</a></p> <pre><code>var i , result = [], , current , propCounter , 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 } }], // get the milestone in a function getMileStone = function(obj) { propCounter = 1; for(propCounter = 1; propCounter &lt;= 7; propCounter++) { // if m1, m2 and so on exists, return that value if(obj.timeline["m" + propCounter]) { return {key: "m" + propCounter, value: obj.timeline["m" + propCounter]}; } } }; // loop over content array (seems like you have an array of objects) for(i=0;i&lt; content.length;i++) { current = content[i]; firstMileStone = getMileStone(current); result.push({ 'start': new Date(current.epc || firstMileStone.value), 'end': new Date(current.m1 || firstMileStone.value), 'content': firstMileStone.key, 'group' : current.name, 'classname' : firstMileStone.value }); } </code></pre> <p>EDIT: getMileStone is just a helper-function, so you could just call it with whatever you want. e.g. current[i+1]:</p> <pre><code>secondMileStone = getMileStone(current[i + 1]) </code></pre> <p>you should just check, if you are not already at the last element of your array. if so current[i+1] is undefined, and the helperfunction should return undefined.</p> <p>you could then use as fallback the firstMileStone:</p> <pre><code>secondMileStone = getMileStone(current[i + 1]) || firstMileStone; </code></pre> <p>see the updated fiddle (including check in the getMileStone-Helperfunction): <a href="http://jsfiddle.net/K37Fa/6/" rel="nofollow">http://jsfiddle.net/K37Fa/6/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload