将数据添加到 JSON 中,用于不存在的日期
Add data to JSON for dates which are not there
我使用 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填充前端的空白日期...
您应该能够在开始日期中添加一天,直到到达结束日期以填充数组,然后替换您拥有的数据的日期。您还可以在填充时检查数据库数据中是否有日期,但这既快速又脏,我想显示地图和过滤器......
// 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>
相关文章:
- 正在将事件处理程序添加到不存在的类
- javascript如果图像不存在don't加载它
- 由于响应中不存在“Access Control Allow Origin”标头,跨域请求停止工作
- JavaScript-如果以前不存在文本,如何从文本区域删除新行
- 如何将一个函数附加到一个不存在的元素上
- 在scala或scalajs Diode中,现有类型中的任何一种都符合“;更新一个没有'还不存在”;
- 查找数组's按属性不存在于另一个数组中的对象
- 使变量可用于不带闭包的异步调用
- 用javascript在对象上创建不存在的方法
- 仅当值不为'不存在
- casperjs-css选择器存在,但当单击它时会引发“”;CasperError:无法在不存在的选择器“”上调度mou
- 运行时不存在Javascript函数
- 使用webdriver和selenium验证元素是否不存在
- 如果文件不存在,fs.watch是否有错误处理程序
- 试图把注意力集中在一个不重要的元素上是不是一种糟糕的做法;不存在
- Backbonejs:在视图中渲染的模型,但在集合中不存在
- 从params推送到对象(如果不存在)
- 在'用于'如果javascript中不存在对象属性,则循环
- 将数据添加到 JSON 中,用于不存在的日期
- 在ipad上不存在用于safari的getBoundingClientRect().需要变通办法