Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript从数组中获取对象_Javascript_Php_Jquery_Arrays_Object - Fatal编程技术网

使用javascript从数组中获取对象

使用javascript从数组中获取对象,javascript,php,jquery,arrays,object,Javascript,Php,Jquery,Arrays,Object,我使用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 lengt

我使用
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);

你只想要日期吗?不,我想要日期对象中每个对象的
name
值,如果这有意义的话。对于每一天,我都需要将所有对象的名称值放入DOM中。@ArunPJohny这不会影响我所做的事情。但它不起作用。你说的不是correct@Blaine抱歉,看起来我看错了…虽然
开始
结束
是包含其他键的变量,但结构中的您是正确的。这正是我要找的!谢谢你的帮助@布莱恩很乐意帮忙。我将添加另一个解决方案,您可能会发现在几秒钟内有用。太好了!我如何获得一组特定的日期。假设我想得到20140114的所有值。我怎样才能抓取那个对象,这样我就可以像你在第二个循环中那样迭代>?@Blaine你是在谈论
route\u date
还是日期键?我想得到日期键。当我创建数组时,我把它做成了一个关联数组,以为我会在JS中使用数组,但事实似乎并非如此,所以我对如何获取特定日期的所有对象(日期键)有点迷茫