0

I have an array with object (date and value), problem is few date objects are missing from that array. I want to full fill that array with every date based on Start Date and End date, also we should consider inner dates, so if a date not exists after one day then that day object should be added after that with the value of null.

I have created a stackblitz for that : https://stackblitz.com/edit/js-pfbcxz In that code I have added different date objects as well as same date object with different time. I wrote some code but that is not fullfilling my requirement. Only few objects are inserting.

5
  • Hi there, what do you mean by 'a date not exists after one day'? Commented Apr 12, 2019 at 2:15
  • so, I want that array fullfilled with every date based on start date and end date in that array, and we should consider inner dates also. Commented Apr 12, 2019 at 2:17
  • I see.. So if the particular date does not have a value, you will need to assign null to the y property? Commented Apr 12, 2019 at 2:28
  • yes. only for the date we are inserting dynamically we can put value as null Commented Apr 12, 2019 at 2:35
  • Sorry for getting back late, i have written an almost complete solution (I think). You can run it and it will give you an appropriate answer. Commented Apr 12, 2019 at 3:51

2 Answers 2

1

The other solution uses moment.js, but what I will be writing for my solution uses pure, Vanilla JavaScript. Basically, we will be using the while loop to loop through the array, and then we check if there are any difference between the current index's date and the date of the previous index. If there is a difference, we will be adding the dates in between, with a null value on y.

const json_data = [{
x: "2018-06-21T20:30:00Z",
y: 6.39
  },
  {
x: "2018-07-21T10:30:00Z",
y: 6.39
  },
  {
x: "2018-07-21T09:30:00Z",
y: 6.39
  },
  {
x: "2018-08-21T21:30:00Z",
y: 5.93
  },
  {
x: "2018-09-21T21:30:00Z",
y: 5.93
  }
];
const getDates = (startDate, stopDate) => {
  const dateArray = [];
  let counterDate = startDate;
  const stopCounter = new Date(stopDate.setDate(stopDate.getDate() -1));
  while (counterDate < stopCounter) {
dateArray.push(counterDate);
counterDate = new Date(counterDate.setDate(counterDate.getDate() + 1));
  }
  return dateArray;
}
//console.log(getDates(new Date(json_data[0].x),new Date(json_data[1].x)))
let k = 1;
const result = [];
while (k < json_data.length) {
  const inBetween = getDates(new Date(json_data[k - 1].x),new Date(json_data[k].x)).map(date => ({
x: date.toISOString(),
y: null
  }));
  //console.log(inBetween)
  if (inBetween.length > 0) {
result.push(json_data[k-1], ...inBetween)
  }
  if (k === json_data.length - 1) {
result.push(json_data[k]);
  }
  k++;
}
console.log(result)

Sign up to request clarification or add additional context in comments.

5 Comments

Actually, did you notice that in result the last object y value is null, but in given array there is value for it
Ok, I get it now. So in the array, what is the expected value of the last 2 indexes?
Is it { "x": "2018-09-20T21:30:00.000Z", "y": null }, { "x": "2018-09-21T21:30:00.000Z", "y": 5.93 } ?
@Swamsreggae Ok I have just edited my code such that getDates will calculate the array of dates exclusive of the starting and end date.
@Swamsreggae All the best, and enjoy!
1

Try converting the list of objects to a simple object

and then search the key (date) in the object.

If not found create the key with null

var json_data = [
{x: "2018-06-21T20:30:00.000Z", y: 6.39},
{x: "2018-07-21T10:30:00.000Z", y: 6.39},
{x: "2018-07-21T09:30:00.000Z", y: 6.39},
{x: "2018-08-21T21:30:00.000Z", y: 5.93},
{x: "2018-09-21T21:30:00.000Z", y: 5.93}
];
var obj = json_data.reduce((acc, data) => {
    acc[data.x] = data.y;
    return acc;
}, {});
var firstDate = new Date(json_data[0].x);
var secondDate = new Date(json_data[json_data.length-1].x);
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var diffDays = Math.round(Math.abs((firstDate.getTime() - 
secondDate.getTime())/(oneDay)));
let k=0;
while(k < diffDays) {
    let nextDay = new Date(new Date(firstDate).getTime() + (k * oneDay));  
    if(obj[nextDay] === undefined) {
      obj[nextDay.toISOString()] = null
    }
    k++      
}
var res = Object.entries(obj).map(item => ({
    x: item[0],
    y: item[1],
}));
console.log(res);

You can use moment.js library to do any kind of date operation.

Add days to date

Check a date is same or before another date

Here is the solution using moment.js

var json_data = [
{ x: "2018-06-21T20:30:00Z", y: 6.39 },
{ x: "2018-07-21T10:30:00Z", y: 6.39 },
{ x: "2018-07-21T09:30:00Z", y: 6.39 },
{ x: "2018-08-21T21:30:00Z", y: 5.93 },
{ x: "2018-09-21T21:30:00Z", y: 5.93 }
];
var obj = json_data.reduce((acc, data) => {
   acc[data.x] = data.y;
   return acc;
}, {});
var start = moment("2018-06-21T20:30:00Z");
var end = moment("2018-09-21T21:30:00Z");
while (start.isSameOrBefore(end)) {
   if (obj[start] === undefined) {
      obj[start] = null;
   }
   start = start.add(1, "days");
}
var res = Object.entries(obj).map(item => ({
   x: item[0],
   y: item[1],
}));
console.log(JSON.stringify(res))

1 Comment

I have created a stackblitz for your answer stackblitz.com/edit/js-kyumu2 In results the last object if you observe date is x: "2018-09-20T20:30:00.000Z" incorrect as it is given in the array date is {x: "2018-09-21T21:30:00.000Z", y: 5.93}. the last object date is not matching and the y value also null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.