-1

I have a sample array below. I want to check in every hour if I have a object at last minute and if its not there I want to push one. For example, in the below array we see that there is an object at 02:55 and 03:50 but I do not have an object at 00:50 and 01:50. SO how can I push an object for those times.

[{day: "2016-10-27T00:45:00.000Z", time: "00:45", val: 100}
{day: "2016-10-27T02:00:00.000Z", time: "02:00", val: 100}
{day: "2016-10-27T02:30:00.000Z", time: "02:30", val: 100}
{day: "2016-10-27T02:55:00.000Z", time: "02:55", val: 100}
{day: "2016-10-27T03:00:00.000Z", time: "03:00", val: 100}
{day: "2016-10-27T03:00:00.000Z", time: "03:00", val: 100}
{day: "2016-10-27T03:00:00.000Z", time: "03:00", val: 100}
{day: "2016-10-27T03:00:00.000Z", time: "03:00", val: 100}
{day: "2016-10-27T03:15:00.000Z", time: "03:15", val: 100}
{day: "2016-10-27T03:30:00.000Z", time: "03:30", val: 100}
{day: "2016-10-27T03:30:00.000Z", time: "03:30", val: 100}
{day: "2016-10-27T03:40:00.000Z", time: "03:40", val: 100}
{day: "2016-10-27T03:50:00.000Z", time: "03:50", val: 100}
{day: "2016-10-27T04:00:00.000Z", time: "04:00", val: 100}]

Can somebody help me with this please.

user6860877
  • 47
  • 1
  • 2
  • 6
  • http://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index – John Boker Oct 26 '16 at 19:31
  • How do you define "missing"? It's trivial to do it in this case if you just insert something as the second item in the array, but I'm guessing you want a more general answer. – VLAZ Oct 26 '16 at 19:31
  • I read this as "I need to add an object to this array and then sort the array by time, ascending". Is that correct. If so, this might help: http://stackoverflow.com/a/10124053/1195835 – TecBrat Oct 26 '16 at 19:33
  • @vlaz you are right I am having tough time finding an object missing at in a given array – user6860877 Oct 26 '16 at 19:53
  • @TecBrat I already have a sorted array. I am having missing values randomly at different hours and minutes so I want to add a point very hour with val 80 – user6860877 Oct 26 '16 at 19:55

3 Answers3

0

Adds new object to array:

arr.push({day: "2016-10-27T01:00:00.000Z", time: "01:00", val: 80})
CroMagnon
  • 1,218
  • 7
  • 20
  • 32
0

Adds new object to array and sorts it on date

arr.push({day: "2016-10-27T01:00:00.000Z", time: "01:00", val: 80});
arr.sort((a,b) => Date.parse(a.day)-Date.parse(b.day));
kevin ternet
  • 4,514
  • 2
  • 19
  • 27
  • Thank Kevin for this answer. But I am trying to push an object every hour like 01:00, 02:00,03:00 and so on. If I have value missing at 6:00 then I want to push an object with a value at that particular hour – user6860877 Oct 26 '16 at 19:59
  • Kevin's solution is exactly what I suggested. Just do this for each time that is missing. pablopunk has given you a function to see if the value is missing. – TecBrat Oct 26 '16 at 20:04
0

You can use a forEach loop:

var foundIt = false;
myArray.forEach(function(element) {
  if (element.time == whatEverIWant) { // missing time
    foundIt = true;
  }
}

if (!foundIt) {
  myArray.push({ /* insert the missing object here */ });
}

// optionally, sort the array by time
myArray.sort(function(a,b) { return a.time.localeCompare(b.time); }

In case you wanna do this for multiple values, just run the code inside another for loop with each value.

pablopunk
  • 371
  • 1
  • 11