.each()函数显示第一个对象3次,而不是所有3个对象

时间:2013-08-23 17:47:41

标签: jquery

查询我的WordPress自定义字段,生成此数组:

Array ( [genre] => Rock [concert_city] => New York [concert_date] => 01-16-2014 [start_time] => 8:00 PM )   Array ( [genre] => Jazz [concert_city] => Chicago [concert_date] => 12-12-2013 [start_time] => 7:00 PM )  Array ( [genre] => Pop [concert_city] => Los Angeles [concert_date] => 11-16-2013 [start_time] => 8:00 PM ) 

此数组存储在:

$array = array();

需要将这些数据导入jQuery。

       $(document).ready(function() {
            var event = <?php echo json_encode($array) ?>;

            $.each(event,function( index, value ){
               console.log(event);
            });
        });

这只显示了我第一个对象3次,而不是所有3个对象。如何迭代所有三个对象?

然后我需要将它们从index:value更改为value(concert_date):value(concert_city),但是现在我只是在我的数组中只获取了第3个对象而不是3个对象。 / p>

编辑:我运行了console.log(value),它打印了第一个数组中的值,其他两个数组都没有。

2 个答案:

答案 0 :(得分:2)

你不是说

$(document).ready(function() {
    var event = <?php echo json_encode($array) ?>;

    $.each(event, function( index, value ){
        console.log(value); // use 'value' not 'event'.
    });
});

答案 1 :(得分:0)

你为什么打印这个活动?根据您的要求打印索引/值:

 console.log(value);