Javascript 当对象具有相同的相同值时,数组不推送
Javascript Array not pushing when having the same same value to object
对象忽略具有相同值的其他数组。
例如
data[2018][2][25] <-- this ones gets ignored to the object
data[2018][2][22]
代码:
var date = new Date();
var data = {};
<?php $eventsNum = 3>
<?php for ($r =1; $r <= 3; $r++):?>
data[<?php echo $calendarYear[$r]?>] = {};
<?php for ($s =1; $s <= 3; $s++):?>
data[<?php echo $calendarYear[$r]?>][<?php echo $calendarMonth[$s]?>] = {};
<?php for ($t =1; $t <= 2; $t++):?>
data[<?php echo $calendarYear[$r]?>][<?php echo $calendarMonth[$s]?>][<?php echo $calendarDay[$s] ?>] = {};
//$num = $calendarDay[$s];
try {
data[<?php echo $calendarYear[$r]?>][<?php echo $calendarMonth[$s]?>][<?php echo $calendarDay[$s] ?>].push({
startTime: "<?php echo $calendarStart_time[1]?>",
endTime: "<?php echo $calendarEnd_time[1] ?>",
text: "<?php echo $calendar_description[1] ?>"
问题在于,每次通过循环,您都会完全替换 属性 中的现有对象。变化:
data[<?php echo $calendarYear[$r]?>] = {};
至:
if (!data[<?php echo $calendarYear[$r]?>]) {
data[<?php echo $calendarYear[$r]?>] = {};
}
所有其他初始化也类似。
对象忽略具有相同值的其他数组。 例如
data[2018][2][25] <-- this ones gets ignored to the object
data[2018][2][22]
代码:
var date = new Date();
var data = {};
<?php $eventsNum = 3>
<?php for ($r =1; $r <= 3; $r++):?>
data[<?php echo $calendarYear[$r]?>] = {};
<?php for ($s =1; $s <= 3; $s++):?>
data[<?php echo $calendarYear[$r]?>][<?php echo $calendarMonth[$s]?>] = {};
<?php for ($t =1; $t <= 2; $t++):?>
data[<?php echo $calendarYear[$r]?>][<?php echo $calendarMonth[$s]?>][<?php echo $calendarDay[$s] ?>] = {};
//$num = $calendarDay[$s];
try {
data[<?php echo $calendarYear[$r]?>][<?php echo $calendarMonth[$s]?>][<?php echo $calendarDay[$s] ?>].push({
startTime: "<?php echo $calendarStart_time[1]?>",
endTime: "<?php echo $calendarEnd_time[1] ?>",
text: "<?php echo $calendar_description[1] ?>"
问题在于,每次通过循环,您都会完全替换 属性 中的现有对象。变化:
data[<?php echo $calendarYear[$r]?>] = {};
至:
if (!data[<?php echo $calendarYear[$r]?>]) {
data[<?php echo $calendarYear[$r]?>] = {};
}
所有其他初始化也类似。