将数据添加到 JSON 中,用于不存在的日期

Add data to JSON for dates which are not there

本文关键字:用于 不存在 日期 数据 添加 JSON      更新时间:2023-09-26

我使用 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>