当对象具有相同的相同值时,Javascript数组不推送

时间:2018-02-25 01:06:34

标签: javascript php arrays object push

对象忽略具有相同值的其他数组。 例如

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] ?>"

1 个答案:

答案 0 :(得分:0)

问题是每次循环都会完全替换该属性中的现有对象。变化:

data[<?php echo $calendarYear[$r]?>] = {};

为:

if (!data[<?php echo $calendarYear[$r]?>]) {
    data[<?php echo $calendarYear[$r]?>] = {};
}

和所有其他初始化类似。