【问题标题】:Javascript Array not pushing when having the same same value to object当对象具有相同的值时,Javascript数组不推送
【发布时间】:2018-02-25 01:06:34
【问题描述】:

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

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

【问题讨论】:

  • 您的代码在语句中间结束。

标签: javascript php arrays object push


【解决方案1】:

问题在于,每次通过循环时,您都会完全替换该属性中的现有对象。变化:

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

到:

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

对于所有其他初始化也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 2021-02-27
    • 2019-08-18
    相关资源
    最近更新 更多