MonkeyBusiness
MonkeyBusiness

Reputation: 593

Add data to JSON for dates which are not there

I use fullcalendar.io and I need to put prices on all dates beetween. Some prices I get from database but some fixed which are not in database I need to create at frontend so with javascript (I think that will be faster and I dont fill database with stupid fixed data, becouse just dates will be different)

First I have start and end date:

var period_start = new Date('2016-02-04 15:18:46');
var period_end = new Date('2016-11-04 15:18:46');

Also I get from database this JSON:

[
  {
    "id": 5,
    "user_id": 1,
    "article_id": 5,
    "price": 78,
    "comment": "",
    "created_at": "2016-02-04 18:33:41",
    "updated_at": "2016-02-04 18:33:41",
    "key": "xLetirBT8wfrTNoQgqkeBUu8ebmZld",
    "title": "",
    "start": "2016-03-25 15:18:46"
  },
  {
    "id": 4,
    "user_id": 1,
    "article_id": 5,
    "price": 55,
    "comment": "",
    "created_at": "2016-02-04 18:33:33",
    "updated_at": "2016-02-04 18:33:33",
    "key": "xLetirBT8wfrTNoQgqkeBUu8ebmZld",
    "title": "",
    "start": "2016-03-15 15:18:46"
  },
  {
    "id": 3,
    "user_id": 1,
    "article_id": 5,
    "price": 35,
    "comment": "",
    "created_at": "2016-02-04 18:33:25",
    "updated_at": "2016-02-04 18:33:25",
    "key": "xLetirBT8wfrTNoQgqkeBUu8ebmZld",
    "title": "",
    "start": "2016-03-07 15:18:46"
  },
  {
    "id": 2,
    "user_id": 1,
    "article_id": 5,
    "price": 22,
    "comment": "dd",
    "created_at": "2016-03-04 15:20:36",
    "updated_at": "2016-02-04 15:20:36",
    "key": "xLetirBT8wfrTNoQgqkeBUu8ebmZld",
    "title": "drugi",
    "start": "2016-03-05 15:18:46"
  },
  {
    "id": 1,
    "user_id": 1,
    "article_id": 5,
    "price": 44,
    "comment": "gg",
    "created_at": "2016-05-04 15:18:46",
    "updated_at": "2016-02-04 15:18:46",
    "key": "xLetirBT8wfrTNoQgqkeBUu8ebmZld",
    "title": "prvi",
    "start": "2016-02-04 15:18:46"
  }
]

Now I want to fill this JSON with dates which are not in JSON file so all dates between period_start and period_end who missing...

so etc. I have in JSON - start: 2016-02-05 but missing other dates...

How to put empty dates into JSON and just change the date - start ?

Also if you have some better solution please tell me, I think that the best is to fill empty dates at frontend with javascript-jquery...

Upvotes: 2

Views: 135

Answers (1)

Neil Twist
Neil Twist

Reputation: 1159

You should be able to add a day to the start date until you get to the end date to populate an array, then replace the dates for the data you have. You could also check if you have a date in the db data as you populate, but this was quick and dirty and I wanted to show map and filter...

// declare variables
var period_start = new Date('2016-02-04 15:18:46'),
    period_end = new Date('2016-11-04 15:18:46'),
    current_date = period_start,
    array_of_all_dates = [];

// Create a populated array of dates
while (current_day.getTime() <= period_end.getTime()) {
    array_of_all_dates.push(current_date);
    current_date = new Date(+current_date);
    current_date.setDate(current_date.getDate() + 1);
}

// Now loop over the array of populated dates and mutate, so something like
array_of_all_dates = array_of_all_dates.map(function (date) {
    var found_in_db = db_data.filter(function (db_data) {
        return new Date(db_data.start).getTime() === date.getTime(); // You need to do this comparison better!
    });
    if (found_in_db.length > 0) {
        return found_in_db[0];
    }
    var new_object = {
        a_property: 'some_default_value',
        start: date
    };
    return new_object;

});

I'm sure this could be done better, but technique is ok if you ignore the time parts and significantly improve the date comparison.

You should probably look into array operations if you don't recognise filter and map.

I've added some extra into the snippet for the purposes of looking at the objects.

var addToList = function (text) {
  var node = document.createElement("LI");
  var textnode = document.createTextNode(text);
  node.appendChild(textnode);
  document.getElementById("myList").appendChild(node);
};




//var db_data = [{"start": "2016-05-04T15:18:46", "title": "FOUND"}, {"start": "2016-06-04T15:18:46", "title": "FOUND ME TOO"}];
var db_data = [
  {
    "id": 5,
    "user_id": 1,
    "article_id": 5,
    "price": 78,
    "comment": "",
    "created_at": "2016-02-04 18:33:41",
    "updated_at": "2016-02-04 18:33:41",
    "key": "xLetirBT8wfrTNoQgqkeBUu8ebmZld",
    "title": "",
    "start": "2016-03-25 15:18:46"
  },
  {
    "id": 4,
    "user_id": 1,
    "article_id": 5,
    "price": 55,
    "comment": "",
    "created_at": "2016-02-04 18:33:33",
    "updated_at": "2016-02-04 18:33:33",
    "key": "xLetirBT8wfrTNoQgqkeBUu8ebmZld",
    "title": "",
    "start": "2016-03-15 15:18:46"
  },
  {
    "id": 3,
    "user_id": 1,
    "article_id": 5,
    "price": 35,
    "comment": "",
    "created_at": "2016-02-04 18:33:25",
    "updated_at": "2016-02-04 18:33:25",
    "key": "xLetirBT8wfrTNoQgqkeBUu8ebmZld",
    "title": "",
    "start": "2016-03-07 15:18:46"
  },
  {
    "id": 2,
    "user_id": 1,
    "article_id": 5,
    "price": 22,
    "comment": "dd",
    "created_at": "2016-03-04 15:20:36",
    "updated_at": "2016-02-04 15:20:36",
    "key": "xLetirBT8wfrTNoQgqkeBUu8ebmZld",
    "title": "drugi",
    "start": "2016-03-05 15:18:46"
  },
  {
    "id": 1,
    "user_id": 1,
    "article_id": 5,
    "price": 44,
    "comment": "gg",
    "created_at": "2016-05-04 15:18:46",
    "updated_at": "2016-02-04 15:18:46",
    "key": "xLetirBT8wfrTNoQgqkeBUu8ebmZld",
    "title": "prvi",
    "start": "2016-02-04 15:18:46"
  }
];

// declare variables
var period_start = new Date('2016-02-04T15:18:46'),
    period_end = new Date('2016-11-04T15:18:46'),
    current_date = period_start,
    array_of_all_dates = [];

// Create a populated array of dates
while (current_date.getTime() <= period_end.getTime()) {
  array_of_all_dates.push(current_date);
  current_date = new Date(+current_date);
  current_date.setDate(current_date.getDate() + 1);
}

// Now loop over the array of populated dates and mutate, so something like
array_of_all_dates = array_of_all_dates.map(function (date) {
  var found_in_db = db_data.filter(function (db_data) {
    return new Date(db_data.start.replace(" ", "T")).getTime() === date.getTime(); // You need to do this comparison better!
  });
  if (found_in_db.length > 0) {
    return found_in_db[0];
  }
  var new_object = {
    a_property: 'some_default_value',
    start: date
  };
  console.log(new_object);
  return new_object;

});

// Display something
array_of_all_dates.forEach(function (date_object) {
  addToList(JSON.stringify(date_object));
});
<ul id="myList">
</ul>

Upvotes: 1

Related Questions