gpt4 book ai didi

javascript - 如果 JSON 数据中不存在,则循环日期并添加对象

转载 作者:行者123 更新时间:2023-11-29 21:34:25 27 4
gpt4 key购买 nike

我需要使用 fullcalendar.io 创建日历 View 。要使用日历,我需要像这样创建一个 JSON:

var db_data = [
{
"id": 5,
"user_id": 1,
"article_id": 5,
"title": "",
"start": "2016-03-25 15:18:46"
},
{
"id": 4,
"user_id": 1,
"article_id": 5,
"price": 55,
"title": "",
"start": "2016-03-15 15:18:46"
} etc.

我需要在从 period_start 到 period_end 的每个日期的日历中设置价格,但是对于某些价格,我在数据库中有值,而对于其他我没有,所以我需要使用标准价格(等 50 美元)...

所以我有 period_start 和 period_end,我从数据库中获取了一些数据,但现在我需要创建一个 JSON,其中包含不在数据库中的日期对象,因此我创建了代码:

function fulljson (){
var db_data;
$.ajax({
url: "http://localhost:8888/diving/{{$article->id}}",
type: "GET",
async: true,
dataType: "json",
success: function(data) {
db_data = data;
console.log(db_data);

// declare variables
var period_start = new Date('{{ date('Y-m-d', strtotime($article->from)) }}'),
period_end = new Date('{{ date('Y-m-d', strtotime($article->to)) }}'),
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(); // I need to do this comparison better!
});
if (found_in_db.length > 0) {
return found_in_db[0];
}
var new_object = {
title: '',
start: date,
price: '{{$article->price}}'
};
console.log(new_object);
return new_object;

});
console.log('result'+array_of_all_dates);
drawCalendar(array_of_all_dates);
},
error: function (data) {
console.log(data);
console.log('ERROR');
}
});

};

$(document).ready(function() {
fulljson();

});

function drawCalendar(data) {
$('#calendar').fullCalendar({
header: {
left: 'prev today',
center: 'title',
right: 'next'
},
defaultDate: Date.now(),

eventRender: function(event, element) {
element.find("fc-event-container").remove();
element.find(".fc-content").remove();
element.find(".fc-event").remove();
var new_description =
'<div style="display:inline-flex; float:right;"><div class="col-md-6"style="margin-top:5px";><p class="price">' + event.price + 'e</p></div>';

element.append(new_description);
} ,// allow "more" link when too many events
events: data,
loading: function(bool) {
$('#loading').toggle(bool);
}
}

});
};

现在这段代码可以在 Chrome 中运行:enter image description here

但在 FireFox 中我得到这个:enter image description here

请看1.2.3。二月,我从数据库中获取数据。您将看到更改。

那么为什么这段代码在 Chrome 中有效而在 Firefox 中无效?什么是问题?有没有更好的方案来解决这个问题?

最佳答案

SO why this code work in Chrome and dont work in Firefox?

因为提供的日期字符串无效,当传递给 new Date() 时,Firefox 会将其转换为 invalid date。然而,Chrome 会解析它们,但这不是跨浏览器的预期行为

从服务器或插件文档中推荐的任何格式发送有效的 ISO 字符串

来自 fullcalender 文档:

When specifying Event Objects for events or eventSources, you may specify a string in IETF format (ex: "Wed, 18 Oct 2009 13:00:00 EST"), a string in ISO8601 format (ex: "2009-11-05T13:15:30Z") or a UNIX timestamp.

关于javascript - 如果 JSON 数据中不存在,则循环日期并添加对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35245791/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com