0

Been hooking with a graph in d3 using time scaling x axis events. Its pointless to explain entire situation with graph but I came in conclusion of a solution. For that I figured out this solution which I am in little confusion with.

Imgine I have an Object:

{
    "values" : [ [ 1136005200000 , 1271000.0] , [ 1138683600000 , 1271000.0] , [ 1141102800000 , 1271000.0] , [ 1143781200000 , 0] , [ 1146369600000 , 0]
}

Lets say it has pairs: [x, y]. x looks like '1136005200000' which is ofcourse a UTC parse date.

What I just need to do is to create a dummy object. In that object, I need to se default dates, lets say 7 days with some parsed numbers. Besides, I need to make sure for the date which doesnt exist in above pair will enter 'y' as 0 and then to finalize the object for 7 days just inserting those values which are available.

Any ideas or approach anyone want to share, please go ahead!

Shobhit Sharma
  • 604
  • 1
  • 9
  • 18

1 Answers1

0

For instance you have two arrays:

var arr1 = [{a: 1, b: 34}, {..}..] upto length 10 var arr2 = [{a:1, b: 0}] upto length < 10

I somehow managed to fix it using these couple of lines after some brainstorming.

Precompute a lookup table which maps 'time' to the replacement 'count'

var lookup = {}, i, e;
for(i = 0, e = arr2[i]; i < arr2.length; e = arr2[++i])
  lookup[e.time] = e.count;

Spin through destination array & merge in the 'count' from the lookup 'arr2'

for(i = 0, e = arr1[i]; i < arr2.length; e = arr2[++i])
  e.count = lookup[e.time] || 0; // Finalizes `arr2` after merging

Assigns zero if value is not there and merge using similar key value pairs.

Shobhit Sharma
  • 604
  • 1
  • 9
  • 18