我使用fullcalendar.io,我需要在所有日期之间加价。我从数据库获得的一些价格,但一些固定不在数据库中我需要在前端创建所以用javascript(我认为这将更快,我不填充数据库与愚蠢的固定数据,因为只是日期将是不同的)
首先我有开始和结束日期:
var period_start = new Date('2016-02-04 15:18:46');
var period_end = new Date('2016-11-04 15:18:46');
我也从数据库得到这个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"
}
]
现在我想用不在JSON文件中的日期来填充这个JSON所以所有日期都在period_start和period_end之间错过了...
所以等我在JSON中 - 开始:2016-02-05但是错过了其他日期...
如何将空日期放入JSON并只更改日期 - start
?
另外如果你有更好的解决方案,请告诉我,我认为最好用javascript-jquery填充前端的空日期......
答案 0 :(得分:1)
您应该能够在开始日期添加一天,直到您到达结束日期以填充数组,然后替换您拥有的数据的日期。您还可以在填充时检查db数据中是否有日期,但这很快且很脏,我想显示 map 和 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;
});
我确信这可以做得更好,但如果你忽略时间部分并显着改善日期比较,技术就可以了。
如果您不识别过滤器和地图,您应该查看数组操作。
为了查看对象,我在代码段中添加了一些额外内容。
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>