繁体   English   中英

使用JavaScript从数组获取对象

[英]Get objects from array with javascript

我使用ajax调用从php脚本获取对象数组; 但是,我无法从中获取嵌套对象。

openings对象如下所示:

Object
2014-01-11: Array[5]
   0: Object
      appointments: "0"
      name: "0"
      openings: "1"
      route_date: "2014-01-11"
__proto__: Object
1: Object
2: Object
3: Object
4: Object
length: 5
__proto__: Array[0]
2014-01-12: Array[5]
2014-01-13: Array[5]
2014-01-14: Array[5]
2014-01-15: Array[5]
2014-01-16: Array[5]
2014-01-17: Array[5]
2014-01-18: Array[5]



length; undefined appointment.js?ver=3.8:58
begin: 20140111 appointment.js?ver=3.8:59
end: 20140228

我试图使用console.log("length; "+openings.length); 获取返回的原始对象的长度,但返回未定义。

这是我的php函数代码:

    function get_available_times()
{
    global $wpdb; 

    $begin = $_REQUEST['begin'];
    $end = $_REQUEST['end']; 
    /*get time slots*/
    $query = "
        SELECT DISTINCT routes.route_date, time_slots.name, time_slots.openings, time_slots.appointments
        FROM routes
        INNER JOIN time_slots ON routes.route_id = time_slots.route_id
        WHERE route_date
        BETWEEN {$begin}
        AND {$end}
        ORDER BY route_date, name
        "; 
    $time_slots = $wpdb->get_results($query);

    /*organize slots into array*/
    $openings = array(); 
    foreach($time_slots as $ts)
    {
        if(empty($openings))
        {
            $openings[$ts->route_date][$ts->name] = $ts; 
        }
        elseif (array_key_exists($ts->route_date, $openings)) 
        {
            $openings[$ts->route_date][$ts->name]=$ts; 
        }
        else
        {
            $openings[$ts->route_date][$ts->name] = $ts; 
        }
    }

    /*return results*/
    $result['openings'] = $openings; 
    $result['time'] = $time_slots;  
    $result['begin'] = $begin; 
    $result['end'] = $end; 
    $result['query'] = $query; 
    $result['type'] = "success"; 
    $result = json_encode($result);
    echo $result;
    die(); 
}

这是我正在使用的Javascript代码:

$.ajax({
            type: "post",
            dataType: "json",
            url: ajaxurl, 
            data:{action: "get_available_times", begin: begin, end:end},
            success: function(response){
                if(response.type == "success"){
                    console.log("testing"); 
                    var openings = response.openings;
                    console.dir(openings);
                    console.log("length; "+openings.length); 
                    console.log("begin: "+response.begin);
                    console.log("end: "+response.end);


                }
            }
        });

所有这些的重点是我希望能够遍历每个日期并将值放入HTML。

好吧,看来您正在获取一个对象,该对象具有5个格式化为日期的属性,每个属性都有其自己的数组值。

因此,尝试这样的事情:

var refDate = new Date(2014, 01, 14);

$.each(response.openings, function(k, opening){

    var parts = k.split('-'), //split the date key
        date = new Date(parts[2], (parts[1] - 1), parts[0]); //parse it do a date

    // jump to next iteration if dates doesn't match
    if(refDate.getTime() !=== date.getTime()) return true;

    $.each(opening, function(k2, v){
        console.log(v.name);
    });
});

您还应该能够将值映射到自定义结果。 这将为您提供所有名称:

var result = $.map(response.openings, function(v){
     return $.map(v, function(v2){
         return { name: v2.name };
     });
});
console.log(result);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM