jquery 转换数组到javascript对象

kwvwclae  于 2023-03-01  发布在  jQuery
关注(0)|答案(2)|浏览(134)

我得到了一个如下所示的数组:

var dateUnvailableCalendar = {{ datesUnvailable|json_encode|raw }};

我想把它变成这样

{
    'from': '2023-03-06 00:00',
    'to': '2023-03-13 00:00'
},
{
    'from': '2023-03-01 00:00',
    'to': '2023-03-01 00:00'
}

你知道我该怎么做吗?
谢谢你的帮助。
工作

calendar.flatpickr({
  disable: [
    function (date) {
      return date.getDay()===dayWeek[0]||
        date.getDay()===dayWeek[1]||
        date.getDay()===dayWeek[2]||
        date.getDay()===dayWeek[3]||
        date.getDay()===dayWeek[4]||
        date.getDay()===dayWeek[5]||
        date.getDay()===dayWeek[6]
    },
    {
      'from': '2023-03-06 00:00',
      'to': '2023-03-13 00:00'
    },
    {
      'from': '2023-03-01 00:00',
      'to': '2023-03-01 00:00'
    }
  ],
  minDate: "today",
  onChange: function (selectedDates, dateStr) {
    calendarHandleChange(dateStr);
  }
});

在这个例子中,将被去激活的日期将是那些在函数中的日期。

calendar.flatpickr({
                        disable: [
                            function(date) {
                                return date.getDay() === dayWeek[0] ||
                                    date.getDay() === dayWeek[1] ||
                                    date.getDay() === dayWeek[2] ||
                                    date.getDay() === dayWeek[3] ||
                                    date.getDay() === dayWeek[4] ||
                                    date.getDay() === dayWeek[5] ||
                                    date.getDay() === dayWeek[6]
                            },
                            [
                                {"from":"2023-03-06 00:00","to":"2023-03-13 00:00"},
                                {"from":"2023-03-01 00:00","to":"2023-03-01 00:00"}
                            ]
                        ],
                        minDate: "today",
                        onChange: function (selectedDates, dateStr) {
                            calendarHandleChange(dateStr);
                        }
                    });

你知道为什么发送数组不起作用吗?

py49o6xq

py49o6xq1#

    • 适用于dateUnvailableCalendar的2个以上元素**

如果您尝试在flatpickr disable属性中使用dateUnvailableCalendar的索引(例如dateUnvailableCalendar[0]),您的问题将得到解决。

let calendar = document.getElementById('calendar');
let dayWeek = [1,1,1,1,1,1,1];

let calendarHandleChange = (dateStr) => {
  console.log(dateStr);
}
let dateUnvailableCalendar = [{
    'from': '2023-03-06 00:00',
    'to': '2023-03-13 00:00'
},
{
    'from': '2023-03-01 00:00',
    'to': '2023-03-01 00:00'
},
{
    'from': '2023-03-22 00:00',
    'to': '2023-03-23 00:00'
}];

calendar.flatpickr({
  disable: [
    function (date) {
      return date.getDay()===dayWeek[0]||
        date.getDay()===dayWeek[1]||
        date.getDay()===dayWeek[2]||
        date.getDay()===dayWeek[3]||
        date.getDay()===dayWeek[4]||
        date.getDay()===dayWeek[5]||
        date.getDay()===dayWeek[6]
    }
  ].concat(dateUnvailableCalendar),
  minDate: "today",
  onChange: function (selectedDates, dateStr) {
    calendarHandleChange(dateStr);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>

<input id="calendar" />
z2acfund

z2acfund2#

看看你在这里做什么:

disable: [
  function(date) { /*...*/ },
  dateUnvailableCalendar
]

dateUnvailableCalendar是什么?它是一个数组吗?如果是,那么得到的结构如下:

disable: [
  function(date) { /*...*/ },
  []
]

也就是说,此disable属性是具有两个元素的数组,其中一个元素是函数,另一个元素是另一个数组。
听起来像是要追加dateUnvailableCalendar数组的 * contents * 以创建一个更大的数组。可以使用spread运算符,例如:

disable: [
  function(date) { /*...*/ },
  ...dateUnvailableCalendar
]
    • 编辑:**功能演示如下:

一个三个三个一个
this answer提供了OP拒绝提供的minimal reproducible example。)

相关问题